virtuexru Posted January 3, 2009 Share Posted January 3, 2009 OK. So I have a string that for example is: <?php $description = "Please call 917-000-0000. Than you."; ?> I want to either just completely strip the "917-000-0000" or replace it with another number. What's the easiest way to go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/139329-solved-how-to-replace-a-numbersection-of-a-string/ Share on other sites More sharing options...
premiso Posted January 3, 2009 Share Posted January 3, 2009 preg_replace Would be the easiest. I am not great at regex but it would be something like: <?php $description = "Please call 917-000-0000. Than you."; $replacement = ""; // replace it with nothing $description = preg_replace('/\d/', $replacement, $description); echo $description; ?> I highly doubt that will work, but that is the idea. Just note the above code will not replace the "-" in the number. Quote Link to comment https://forums.phpfreaks.com/topic/139329-solved-how-to-replace-a-numbersection-of-a-string/#findComment-728717 Share on other sites More sharing options...
virtuexru Posted January 3, 2009 Author Share Posted January 3, 2009 Figured out it using that, thank you. Here was my final code: <?php // remove number $string = $content; $pattern = '/718-555-5454/'; $replacement = "001-203-509-1105"; // replace it with nothing $description = preg_replace($pattern, $replacement, $string); ?> Quote Link to comment https://forums.phpfreaks.com/topic/139329-solved-how-to-replace-a-numbersection-of-a-string/#findComment-728752 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.