Reputation: 343
What regular expression should I use to detect is the text I want to hyperlink had been already hyperlinked.
Example:
I use
$text = preg_replace('/((http)+(s)?:\/\/[^<>\s]+)/i', '<a href="\\0">\\0</a>', $text);
to link regular URL, and
$text = preg_replace('/[@]+([A-Za-z_0-9]+)/', '@<a href="http://twitter.com/#!/\\1">\\1</a>', $text);
to link Twitter handle.
I want to detect whether or not the text I'm going to hyperlink had been wrapped in <a href=""></a>
already.
Upvotes: 1
Views: 125
Reputation: 1
$html = '<a href="#">Stephen Ou</a>';
$str = 'Stephen Ou';
if (strlen(str_replace($str, '', $html)) !== strlen($html)) {
echo 'I got a feeling';
}
Upvotes: 0
Reputation: 1361
Maybe not an answer but another possible solution; You could also search to see if the starting a element exists
$text = '<a href="#">here</a>';
if (gettype(strpos($text, "<a")) == "integer"){
//<a start tag was found
};
or just strip all tags regardless and build the link anyway
$text = '<a href="#">here</a>';
echo '<a href="my-own-url">' . strip_tags($text) . '</a>';
Upvotes: 1
Reputation: 18295
Simple, replace the regular URLs first, as it won't affect anything starting with an @
cause no URL starts with an @
. Then replace the twitter handles.
That way you don't need to detect if it's been hyperlinked already.
Upvotes: 0