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">开发者_Go百科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.
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...
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.
精彩评论