filter link text to have the http [closed]
I've got a script that will cherry pick urls that people have inserted into content.
It's meant to auto link them but the link doesn't when the "http://" is omitted. What is a smart way, with possibly one line of code that I can add it back (if it isn't there)
using LAMP
(strpos($edm['link'], 'http://')!==false)?$edm['link']:'http://'.$edm['link']
is what i tried
$prefix = 'http://';
if (strpos($url, $prefix) !== 0) {
$url = $prefix . $url;
}
Also, your example...
(strpos($edm['link'], 'http://')!==false) ? $edm['link'] : 'http://'.$edm['link'];
...will fail on a matched URL such as...
facebook.com/l.php?u=http://example.com
...which will leave it as...
facebook.com/l.php?u=http://example.com
...which is probably not what you want. Ideone.
However, mine doesn't have that problem :)
My favorite way: first you delete the 'http://' part just in case it's already there, and then append it anyway.
edm['link'] = "http://" . preg_replace('#^http://#','',edm['link']);
Note: This won't work for https, but it's easy to do.
精彩评论