Jump to content

How to manipulate some variables inside preg_replace code ?


adrianTNT

Recommended Posts

Hello, I have a code that creates <a> tags (clickable links) from plain text urls. This code searches for http:// and www and creates the <a> tags.

 

$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" rel=\"nofollow\" target=\"_blank\">\\2</a>", $ret);

 

How can I truncate the \\2 before the </ a>? So that it doesn't break my layout. I have a truncate_text() function but I don't know how to insert it there.

 

Thank you.

 

I dont think str_replace can do that in order to find patterns like http:// and www, can it? I am completely lost when I try to understand those things like:

("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)

 

:(

 

Any other ideas?

By truncating \\2 I mean that the anchor text of an < a > tag will get shorter, so that the link will not be too large in the page and damaging the layout.

 

Can someone post a sample for the str_replace ? Because as I know that one will only replace an exact string and I cannot see a way to create the link html code with that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.