0

We have content of a page which has multiple instances of get_template_part in them. We want to grab them in an array do eval on them so we could include those template parts as well.

The problem is we know the beginning "get_template_part" and the ending "');" but the ending is the same for a lot of similar instances we don't want to grab.

How do we grab only the ending of get_template_part and disclude all others

Example of a String we want to include: <?php get_template_part( 'template-parts/breadcrumbs' ); ?>

I tried the following function which accepts a haystack and 2 needles and returns the matching string but then again the second needle is no way exclusive to the first needle

function get_substring_part($haystack, $needle, $needle2, $find) {

$lastPos_one = 0;
$lastPos_two = 0;
$positions_one = array();
$positions_two = array();

while (($lastPos_one = strpos($haystack, $needle, $lastPos_one))!== false) {
    $positions_one[] = $lastPos_one;
    $lastPos_one = $lastPos_one + strlen($needle);
}
foreach ($positions_one as $value_start) {
    $return[]['start'] =  $value_start;
}

while (($lastPos_two = strpos($haystack, $needle2, $lastPos_two))!== false) {
    $positions_two[] = $lastPos_two;
    $lastPos_two = $lastPos_two + strlen($needle2);
}
foreach ($positions_two as $value_end) {
    $return[]['end'] =  $value_end;
}

$counter = 0;
foreach ($return as $positions) {
    $string['start'] = $positions['start'];
    $string['end'] = $positions['end'];
    $var = substr($haystack, $positions['start'], $positions['end']);


    if (strpos($var, $find) !== false) {
        $string['string'] = substr($haystack, $positions['start'], $positions['end']);
    }
    $counter++;
}

return $string;
}

Im using this function with the following attributes:

get_substring_part($content, 'get_template_part', ');', 'get_template_part')
3
  • 1
    Do you have some examples? What have you tried so far? Commented Feb 4, 2020 at 13:46
  • Please add a sample string with what you are trying to grab and what to exclude Commented Feb 4, 2020 at 13:50
  • Can you show all the instances of get_template_part at once and show the desired result? Commented Feb 4, 2020 at 14:06

1 Answer 1

1

The full matches will be in $matches[0] and what is in between the ' ' will be in $matches[1]. You can move the ( ) to get more or less and add the <?php if you need it:

preg_match_all("/get_template_part\s*\(\s*'(.+?)'\s*\);?/", $template, $matches);
print_r($matches);
  • Match get_template_part then \s* space 0 or more
  • Then ( then \s* space 0 or more
  • Then ' then (.+?) capture . characters +? 1 or more lazy
  • Then ' then \s* space 0 or more then ) then ; optional ?
Sign up to request clarification or add additional context in comments.

6 Comments

Worked like a charm. Let me debug this more and ill mark this as correct
Let's hope you won't have things such as get_template_part(generate_part()), or get_template_part('look/someone/put/brackets()')
All working fine expect these: <?php get_template_part( 'template-parts/frontpage/section', 'popular' ) ?> <?php get_template_part( 'template-parts/frontpage/section', 'videos' ) ?>
@Cid: Actually the parentheses don't cause a problem. Maybe the absence of quotes ' but they showed them in the question.
a PHP parser would maybe suits rather this need rather than RegEx
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.