tags:

views:

57

answers:

4

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"&gt;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&lt;/a&gt;&lt;/p&gt;

Thanks!

A: 

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.

Josh Curren
A: 

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...

David Thomas
+5  A: 

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"&gt;My Link</a>');

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

metrobalderas
The only downside for this approach is the regex can be bypassed by the users and is possible to break as you hard coded the matched parts. But it does solve the problem. +1
Jay Zeng
Well, it's a quick fix. Also, it won't match if the link is formulated with single quotes, but you get the idea.
metrobalderas
Another quickfix... Everybody need a little of SEO: return "{$m[5]} <a href=\"{$m[2]}//{$m[3]}\" rel=\"nofollow\" title=\"{$m[5]}\">{$m[2]}//{$m[3]}</a>";
TiuTalk
+1  A: 

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

Ignacio Vazquez-Abrams