Jeffro Posted April 25, 2011 Share Posted April 25, 2011 I'm trying to do a string replace on this phrase: "all 6 news updates" ... keeping in mind that the number 6 could be 30 or 100 or 1000 or whatever... How do I go about that? I know how when the text is static but since the number is changing, I dont. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/234681-how-do-do-a-str_replace-on-this/ Share on other sites More sharing options...
wildteen88 Posted April 25, 2011 Share Posted April 25, 2011 You'll want to use regex $str = "all 6 news updates"; $number = preg_replace('/all (\d+) news updates/', '$1', $str); echo "Echo the number is: $number"; What do you want replace the number with? Quote Link to comment https://forums.phpfreaks.com/topic/234681-how-do-do-a-str_replace-on-this/#findComment-1205992 Share on other sites More sharing options...
Jeffro Posted April 25, 2011 Author Share Posted April 25, 2011 You'll want to use regex $str = "all 6 news updates"; $number = preg_replace('/all (\d+) news updates/', '$1', $str); echo "Echo the number is: $number"; What do you want replace the number with? Actually, I'm not wanting to replace the number. I want to find all instances of that entire phrase (regardless of the number that I find) and blank it out. $description=str_replace('all 6 news updates','',$description) ...keeping in mind that the number could be anything, which is why I can't use the code as I've just written it. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/234681-how-do-do-a-str_replace-on-this/#findComment-1206026 Share on other sites More sharing options...
wildteen88 Posted April 25, 2011 Share Posted April 25, 2011 $str = "blah blah blah blah all 6 news updates blah blah blah all 999 news updates blah"; $str = preg_replace('/all (\d+) news updates/', '', $str); echo nl2br($str); Quote Link to comment https://forums.phpfreaks.com/topic/234681-how-do-do-a-str_replace-on-this/#findComment-1206030 Share on other sites More sharing options...
Jeffro Posted April 25, 2011 Author Share Posted April 25, 2011 $str = "blah blah blah blah all 6 news updates blah blah blah all 999 news updates blah"; $str = preg_replace('/all (\d+) news updates/', '', $str); echo nl2br($str); Thank you, sir. Worked great! Quote Link to comment https://forums.phpfreaks.com/topic/234681-how-do-do-a-str_replace-on-this/#findComment-1206045 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.