EchoFool Posted January 23, 2010 Share Posted January 23, 2010 Hey i have a string replace for talk like a pirate day but it doesnt work how i was hopeing : <?php $text = str_replace(' hey ', 'Yo ho ho, and a bottle of rum!', $text); ?> This currently only works if there is a space either side of "hey" so " hey " is valid "hey" is not valid... i could only do it this way because otherwise words with "hey" in the middle of the word would be replaces such as: So "they" became: "tYo ho ho, and a bottle of rum!" which is annoying - how do i solve that issue? Link to comment https://forums.phpfreaks.com/topic/189494-str_replace/ Share on other sites More sharing options...
teamatomic Posted January 23, 2010 Share Posted January 23, 2010 Its called word boundary /\bhey\b/ put an i after it for case insensitivity /\bweb\b/i HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/189494-str_replace/#findComment-1000249 Share on other sites More sharing options...
EchoFool Posted January 23, 2010 Author Share Posted January 23, 2010 I tried: $text = str_replace('/\byo\b/i', 'Yo ho ho, and a bottle of rum!', $text); But that didnt work either :S Link to comment https://forums.phpfreaks.com/topic/189494-str_replace/#findComment-1000251 Share on other sites More sharing options...
teamatomic Posted January 23, 2010 Share Posted January 23, 2010 Just use preg_replace instead $text='Yo ho ho, and a duncan yoyo!'; $text= preg_replace("/\byo\b/i", 'JoJo!',"$text"); echo "$text"; HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/189494-str_replace/#findComment-1000258 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.