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?
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);
$string = preg_replace('/\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $string);
See Daring Fireball's An Improved Liberal, Accurate Regex Pattern for Matching URLs
Excerpt:
(?xi)
\b
( # Capture 1: entire matched URL
(?:
[a-z][\w-]+: # URL protocol and colon
(?:
/{1,3} # 1-3 slashes
| # or
[a-z0-9%] # Single letter or digit or '%'
# (Trying not to match e.g. "URI::Escape")
)
| # or
www\d{0,3}[.] # "www.", "www1.", "www2." … "www999."
| # or
[a-z0-9.\-]+[.][a-z]{2,4}/ # looks like domain name followed by a slash
)
(?: # One or more:
[^\s()<>]+ # Run of non-space, non-()<>
| # or
\(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels
)+
(?: # End with:
\(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels
| # or
[^\s`!()\[\]{};:'".,<>?«»“”‘’] # not a space or one of these punct chars
)
)
Try this:
/http:\/\/[a-zA-Z0-9\.\/\?\=\_]+/
精彩评论