简体   繁体   中英

Replacing Text link as link with preg_replace

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);

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>

Can you just try this:

function makeClickableLinks($s) {
  return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
}

To test it, try this:

echo makeClickableLinks('Hello world, you go to www.google.com to check! also http://www.google.com/ works too!');

Demo here: http://codepad.viper-7.com/hkOdCM

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