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')
get_template_partat once and show the desired result?