I have a php function that called in this way
breadcrumbs_func();
stamp this breadcrumbs:
<div class="breadcrumbs" itemscopeitemtype="http://schema.org/BreadcrumbList">
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a href="#" itemprop="item"><span itemprop="name">Home</span></a>
<meta itemprop="position" content="nprog">
</span>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a href="#" itemprop="item" ><span itemprop="name">Category</span></a>
<meta itemprop="position" content="nprog">
</span>
</div>
When I call this function I want to replace the two content="nprog"
with a progressive number (1-2-3...) in this way:
content="1"
, content="2"
I tried with str_replace();
but it doesn't seem to work:
$breadcrumbs = breadcrumbs_func();
$new_breadcrumbs = str_replace("nprog", "1", $breadcrumbs);
echo $new_breadcrumbs;
this return the breadcrumbs with no changes.
Any help?
Here is a way :
function str_replace_first($search, $replace, $subject) {
$pos = strpos($subject, $search);
if ($pos !== false) {
return substr_replace($subject, $replace, $pos, strlen($search));
}
return $subject;
}
$new_breadcrumbs = str_replace_first("nprog", "1", $breadcrumbs);
for($i=2; $i<=substr_count($breadcrumbs,"nprog");$i++){
echo $i;
$new_breadcrumbs =str_replace_first("nprog", $i, $new_breadcrumbs);
}
echo $new_breadcrumbs;
Ok I found a solution to store the echo result in the $var:
<?php ob_start();
breadcrumbs();
$breadcrumbs = ob_get_contents();
ob_end_clean();
I hope this will not affect stability or performance!