<?php $names="my site your site not their site"; $a1 = str_replace("site", "<a href='http://forums.digitalpoint.com'>site</a>", "$names"); echo $a1; ?> PHP: result: ---------- how to let str_replace effect one time? i would like below result thank you
PHP documentation says: str_replace — Replace all occurrences of the search string with the replacement string So, to replace only one occurrence you need another way, another function: preg_replace <?php $names="my site your site not their site"; $a1 = str_replace("site", "<a href='http://forums.digitalpoint.com'>site</a>", "$names"); echo $a1; echo "<br /><br /><br />"; $a1 = preg_replace("/site/", "<a href='http://forums.digitalpoint.com'>site</a>", "$names", 1); echo $a1; ?> PHP: