Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove full URL from text

Should be an easy pattern match and replace, but I want to be able to remove a full URL from text.

So:

'You gotta love it! http://www.youtube.com/watch?v=0i_bkLbf3EI check it out!!!'

Becomes:

'You gotta love it! check it out!!!'

Any ideas?

like image 950
thebounder Avatar asked Apr 07 '26 17:04

thebounder


1 Answers

Step 1: Find a regular expression that matches URLs

http://mathiasbynens.be/demo/url-regex

Seems like the last one (@diegoperini) is the best, but it weighs in at 502 characters.

Step 2: Replace any matches of that regex with the empty string

$output = preg_replace($regex, '', $input);
like image 52
Jon Avatar answered Apr 09 '26 07:04

Jon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!