mikefrederick Posted February 13, 2008 Share Posted February 13, 2008 why does this not work? it replaces each $x in the array with the actual word array rather than the original word. $bar = $_GET['letters']; $x=explode(" ",$bar); $a=str_replace($x,"<span style='color: #000066; font-weight: 600'>" . $x . "</span>",$t); Link to comment https://forums.phpfreaks.com/topic/90943-str_replace-clarification/ Share on other sites More sharing options...
cooldude832 Posted February 13, 2008 Share Posted February 13, 2008 because you need to use str_replace on a string variable in your case $x is an arrary Link to comment https://forums.phpfreaks.com/topic/90943-str_replace-clarification/#findComment-466099 Share on other sites More sharing options...
mikefrederick Posted February 13, 2008 Author Share Posted February 13, 2008 how do I do this then? I mean you can use an array for the value you want to replace, why not for the value you are replacing it with Link to comment https://forums.phpfreaks.com/topic/90943-str_replace-clarification/#findComment-466101 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 because $x is an array try this: $a=preg_replace("/\$x/,"<span style='color: #000066; font-weight: 600'>$1</span>",$t); Update: scratch the above...posting another one is sec Link to comment https://forums.phpfreaks.com/topic/90943-str_replace-clarification/#findComment-466103 Share on other sites More sharing options...
cooldude832 Posted February 13, 2008 Share Posted February 13, 2008 PHP replacement type functions or search functions work on a principle of needle in a haystack meaning your needle (must be integers or string mostly) is searched for in a haystack (most var types integers, string, arrays) You must do a foreach and replace each node of $x individually or some alternative method Edit: if that isn't clear your needle is your original string, and the haystack is your query pool what is to be found. I think I stated it backwards:) Link to comment https://forums.phpfreaks.com/topic/90943-str_replace-clarification/#findComment-466104 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 <?php foreach(explode(" ",$bar) as $x) $t = str_replace($x,"<span style='color: #000066; font-weight: 600'>{$x}</span>",$t); ?> Link to comment https://forums.phpfreaks.com/topic/90943-str_replace-clarification/#findComment-466108 Share on other sites More sharing options...
mikefrederick Posted February 13, 2008 Author Share Posted February 13, 2008 thanks rhodesa Link to comment https://forums.phpfreaks.com/topic/90943-str_replace-clarification/#findComment-466123 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.