str_replace() for multiple terms

I have a query with a loop that has a lot of keywords that need to be linked to other sites. I understand that using str_replace can replace one string term with other information like so:

$row_getHHH = str_replace('Membership Form', '<a href="files/docs/membership_form.pdf">Membership Form</a>', $row_getHHH);

But, within my query, I have multiple terms that need to be replaced with links. From this query:

mysql_select_db($database_Tr_L349, $Tr_L349);
$query_getHHH = "SELECT * FROM how_to_help";
$getHHH = mysql_query($query_getHHH, $Tr_L349) or die(mysql_error());
$row_getHHH = mysql_fetch_assoc($getHHH);
$totalRows_getHHH = mysql_num_rows($getHHH);

with this loop:

<?php do { ?>
<h2><?php echo $row_getHHH['heading']; ?></h2>
<?php
$text = nl2br($row_getHHH['content']);
$after = str_replace("<br />", "</p><p>", $text);?>
<p><?php echo $after ; ?></p>
<?php } while ($row_getHHH = mysql_fetch_assoc($getHHH)); ?>

how do I go about changing more than one term? I.E., besides the above string being changed to a link, I have others such as “Special Events” that has an external link to a different page that’s coming from the same query.

I’ve thought about AND/OR, but it wasn’t working. Any suggestions?

Thank you.

str_replace() accepts arrays as its arguments:


$input = "A and B went shopping.";

$search  = array("A",     "B");
$replace = array("Alice", "Bob");

$output = str_replace($search, $replace, $input);

echo $output; // Outputs "Alice and Bob went shopping."

I have this, but not getting the results I’m expecting:

<?php
$text = nl2br($row_getHHH['content']);
$search = array("Membership Form", "Rules for Donating Books");
$replace = array('<a href="files/docs/membership_form.pdf">Membership Form</a>', '<a href="files/docs/book_donation.pdf">Rules for Donating Books</a>');  
$output = str_replace($search, $replace, $text);
$after = str_replace("<br />", "</p><p>", $text);?>
<p><?php echo $after ; ?></p>
<?php } while ($row_getHHH = mysql_fetch_assoc($getHHH)); ?>

Tell if I’m misunderstanding the above statement:

  1. variable $text is the content queried from database
  2. variable $search is an array search for these specific terms
  3. variable $replace is another array to replace the $search array terms
  4. $output is to $search and $replace the terms in the $text variable
  5. $after replaces any <br />s with paragraph marks in the $text variable
  6. $after gets echoed

I’m assuming that after the series of instruction is completed that $after would be echoing all information (including the search and replace) accordingly. Did I forget something?


<?php
$text = nl2br($row_getHHH['content']);
$search = array("Membership Form", "Rules for Donating Books");
$replace = array('<a href="files/docs/membership_form.pdf">Membership Form</a>', '<a href="files/docs/book_donation.pdf">Rules for Donating Books</a>');  
$output = str_replace($search, $replace, $text);

$after = str_replace("<br />", "</p><p>", $output/*$text*/);?>

<p><?php echo $after ; ?></p>
<?php } while ($row_getHHH = mysql_fetch_assoc($getHHH)); ?>

And I was so close! Thank you! :slight_smile: