开发者

filter link text to have the http [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vagu开发者_JAVA百科e, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜