-1

At the top of my page I have a breadcrumb.

ob_start();
<li>##product##</li>
<li>##category##</li>

After this, I run mysql query to get the variables:

$product
$category

then I run:

<?php echo str_replace("##product##", $product, ob_get_clean())  ?>
<?php echo str_replace("##category##", $category, ob_get_clean())  ?>

The variable $product gets replaced and I see the second variable won't get replaced because ive called ob_get_clean()) already for product.

How can I replace both strings using the ob_get_clean function?

user3312792
  • 1,101
  • 2
  • 12
  • 27
  • 2
    `str_replace(["##product##", "##category##"], [$product, $category, ob_get_clean()]);` – Alma Do Jul 30 '14 at 07:05
  • 1
    Why are you doing this? Run your query at the top of the page and generate your breadcrumb correctly in the first place. –  Jul 30 '14 at 07:05
  • U can define a callback function in `ob_start($callback)` – DarkBee Jul 30 '14 at 07:10

6 Answers6

1

Assign the result of ob_get_clean() to a variable:

$ob = ob_get_clean();
$ob = str_replace("##product##", $product, $ob);
$ob = str_replace("##category##", $category, $ob);
echo $ob;
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Use ob_get_contents instead of ob_get_clean.

$product = 'product 1';
$category = 'category1';
ob_start();
echo '<li>##product##</li>
      <li>##category##</li>';


$template = ob_get_contents();

$template = str_replace("##product##", $product, $template);
$template = str_replace("##category##", $category, $template);

ob_end_clean();
echo $template;
cocheese
  • 512
  • 2
  • 9
0

Try this:

$find = array("##product##", "##category##");
$replace = array($product, $category);
echo str_replace($find, $product, ob_get_clean());
Roman Newaza
  • 11,405
  • 11
  • 58
  • 89
Nauphal
  • 6,194
  • 4
  • 27
  • 43
0

The Problem is, you are replcing tho variables from one source.

Best you can do is:

$from = array("##product##", "##category##");
$to = array($product, $category);
echo str_replace($from, $to, ob_get_contents());

Or you can run several functions in one row. Notice $replaced is the same as Input and Output:

$replaced = ob_get_contents();
$replaced = str_replace("##product##", $product, $replaced);
$replaced = str_replace("##category##", $category, $replaced);
echo $replaced;

I thnk, it is effectively to use the first method, because you are calling it only once with data supply.

m1k1o
  • 2,344
  • 16
  • 27
0

Use ob_get_contents() instead :

<?php echo str_replace("##product##", $product, ob_get_contents())  ?>
<?php echo str_replace("##category##", $category, ob_get_contents())  ?>

If you use ob_get_clean(), you delete output buffer after first call.

fdehanne
  • 1,658
  • 1
  • 16
  • 32
0

ob_get_clean() essentially executes both ob_get_contents() and ob_end_clean().

<?php 
    ob_start();
    echo str_replace("##product##", $product, ob_get_contents());
    echo str_replace("##category##", $category, ob_get_contents());
?>
Mohit S
  • 13,723
  • 6
  • 34
  • 69