PTS Posted April 3, 2007 Share Posted April 3, 2007 I'm trying to figure how to make multiple replacements in a string before echoing the string out. str_replace('Chris Drury', '<a href="/profile.php?playerid=23">Chris Drury</a>', $fullstory); The above code does what I need it to do but just for one player. I want to find a way to make it happen for about 30 players. Is there a way to do this in PHP? Thanks in advance for any advice or direction. Link to comment https://forums.phpfreaks.com/topic/45449-multiple-replacements-in-string/ Share on other sites More sharing options...
joquius Posted April 3, 2007 Share Posted April 3, 2007 Well, I would advise using a regular expression to match a name, or, if you have the list before hand, simply make an array: $array = ("/Player1/","/Player2/","/PlayerN/"); and then $text = preg_replace ($array, "<a href=\"\">$1</a>", $text); Link to comment https://forums.phpfreaks.com/topic/45449-multiple-replacements-in-string/#findComment-220670 Share on other sites More sharing options...
pocobueno1388 Posted April 3, 2007 Share Posted April 3, 2007 <?php $sql = mysql_query("SELECT * FROM users_tbl WHERE condition"); while ($row = mysql_fetch_assoc($sql)){ str_replace('$row['name']', '<a href="/profile.php?playerid={$row['ID']}">{$row['name']}</a>', $fullstory); } ?> Link to comment https://forums.phpfreaks.com/topic/45449-multiple-replacements-in-string/#findComment-220671 Share on other sites More sharing options...
PTS Posted April 3, 2007 Author Share Posted April 3, 2007 Thank you sir! I figured it out but your way is 10x easier than what I came up with! It worked like a charm. Link to comment https://forums.phpfreaks.com/topic/45449-multiple-replacements-in-string/#findComment-220797 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.