简体   繁体   中英

Modify hyperlinks with PHP

I'm using a regular expression to turn URLs in blog comments into clickable hyperlinks. However, i also want to do the opposite:

Since i allow certain html tags (but not <a> ), if somebody types in a hyperlink, i'd like to change it from:

<a href="http://www.example.com">My Link</a>

into

My Link : http://www.example.com

where the generated code is:

<p><b>My Link:</b> <a href="http://www.example.com" rel="nofollow">http://www.example.com</a></p>

Thanks!

Try with this.

function find_links($url){
    $pattern = '/<a (.*?)href="(.*?)\/\/(.*?)"(.*?)>(.*?)<\/a>/i';
    $url = preg_replace_callback($pattern, 'process_links',$url);
    return $url;
}

function process_links($m){
    return "{$m[5]} <a href=\"{$m[2]}//{$m[3]}\" rel=\"nofollow\">{$m[2]}//{$m[3]}</a>";
}

$links = find_links('<a href="http://www.example.com">My Link</a>');

EDIT: Oops! I didn't quite gave answer to the OP's question.

Parsing an irregular language with a regular expression is the short road to failure. Use a proper HTML parser instead.

You just need to search for either www or http then convert that text until you reach a space to the url.

Something like:
    $startPos = strpos( $input, "http" );
    $endPos = strpos( $input, " ", $startPos );
    $urlText = substr( $input, $startPos, $endPos - $startPos );

I think I miss read your question a bit... something similar to the above but looking for instead.

Well, if you don't mind some CSS (and the implementation variances of browsers):

a {font-weight: bold; }
a:after {content: " (" attr(href) ") "; }

Should partly achieve your aims, though it won't remove the link while showing the link text. So, really I guess it doesn't. Sorry...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM