Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing Text link as link with preg_replace

Tags:

regex

php

I seem to be having an issue replacing a text link with a link to the site posted, it isn't linking.

The code:

$status_text = preg_replace('#(\A|[^=\]\'"a-zA-Z0-9])(http[s]?://(.+?)/[^()<>\s]+)#i', '\\1<a href="\\2">\\3</a>', $status_text);
echo $status_text;

$status_text is pulled from a MySQL field named contents, and contains other text, but i'd just like to linkify the link. Additionally, i'd also like it to not display the full URL, just the main domain.

UPDATE: We have two other preg_replaces on the same page, looking for things with + and # in front of them that link to areas of the site, they currently work and need to not conflict with the above:

$status_text = preg_replace("/#([a-z_0-9]+)/i", "<a href=\"http://url.com/hashlink/$1\">$0</a>", $status_text);


$status_text = preg_replace("/\+([a-z_0-9]+)/i", "<a href=\"http://url.com/pluslink/$1\">$0</a>", $status_text);
like image 536
mobile Avatar asked Jan 15 '23 05:01

mobile


1 Answers

Here, try this:

$status_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 <a href="$2">$3</a>', $status_text);
echo $status_text;

or to make it a little easer to read:

$m = '|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$r = '$1 <a href="$2">$3</a>';

$status_text = preg_replace($m,$r,$status_text);
echo $status_text;

EDIT -- Updating due to new info in the OP --

Your hashtag regex does not take into account hashes in URLs, so let's fix that... Also, you should match for URLs before matching for hash-tags or plus-tags because otherwise you will mess up the links you create for the hash-tags and plus-tags

$status_text = preg_replace('|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$2</a>', $status_text);
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);

Or to make it a little easier to read...

$match_href = '|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$2</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';

$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);

EDIT AGAIN -- Adding a URL that might be helpful --

You can test out regular expressions here: http://gskinner.com/RegExr/

ANOTHER EDIT

As per a comment/question, if you want to account for urls that lack the http protocol, use the following:

$status_text = preg_replace('|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$3</a>', $status_text);
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);

Or to make it a little easier to read...

$match_href = '|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$3</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';

$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);

EXAMPLE USAGE

Input: (Text from DB -> $status_text)

<!-- language-all: lang-none -->
Hi, this is an example. This is a url http://stackoverflow.com/ 
and this is a hash reference that we want to link to an internal 
post #AwesomePost123 and this one is a plus reference we want to 
link to an internal post +AwesomePost123 and finally an example 
of a url without the http protocol www.stackoverflow.com

Output: (After running through the regex)

<!-- language-all: lang-none -->
Hi, this is an example. This is a url <a href="http://stackoverflow.com/">stackoverflow.com</a> 
and this is a hash reference that we want to link to an internal 
post <a href="http://url.com/pluslink/AwesomePost123">#AwesomePost123</a> and this one is a plus reference we want to 
link to an internal post <a href="http://url.com/pluslink/AwesomePost123">+AwesomePost123</a> and finally an example 
of a url without the http protocol <a href="www.stackoverflow.com">www.stackoverflow.com</a>
like image 156
mason81 Avatar answered Jan 29 '23 11:01

mason81