mcmuney Posted March 27, 2012 Share Posted March 27, 2012 How can I remove certain string from a statement. For example: A blagh blah blah blah blah. http://blah.com Where I would want to remove everything from "http" and after. So the final statement would show as: A blagh blah blah blah blah. Quote Link to comment https://forums.phpfreaks.com/topic/259774-removing-characters/ Share on other sites More sharing options...
QuickOldCar Posted March 27, 2012 Share Posted March 27, 2012 http://php.net/manual/en/function.preg-match.php http://php.net/manual/en/function.preg-match-all.php and matching patterns using regex. Quote Link to comment https://forums.phpfreaks.com/topic/259774-removing-characters/#findComment-1331422 Share on other sites More sharing options...
QuickOldCar Posted March 27, 2012 Share Posted March 27, 2012 Here is 2 easy examples of how to remove everything after a certain character <?php $string = "A blagh blah blah blah blah. http://blah.com"; $removed = explode("http://", $string); $removed = $removed[0]; echo $removed."<br />"; $removed2 = substr($string, 0, strpos("$string*", "http://")); echo $removed2; ?> Quote Link to comment https://forums.phpfreaks.com/topic/259774-removing-characters/#findComment-1331435 Share on other sites More sharing options...
scootstah Posted March 27, 2012 Share Posted March 27, 2012 $str = 'A blagh blah blah blah blah. http://blah.com'; $str = preg_replace('/http:\/\/.*?$/i', '', $str); // $str = "A blagh blah blah blah blah. " Quote Link to comment https://forums.phpfreaks.com/topic/259774-removing-characters/#findComment-1331436 Share on other sites More sharing options...
QuickOldCar Posted March 27, 2012 Share Posted March 27, 2012 Yeah that's the best way scootstah, handles the case as well Quote Link to comment https://forums.phpfreaks.com/topic/259774-removing-characters/#findComment-1331438 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.