adrianTNT Posted January 5, 2008 Share Posted January 5, 2008 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. Link to comment https://forums.phpfreaks.com/topic/84606-how-to-manipulate-some-variables-inside-preg_replace-code/ Share on other sites More sharing options...
gerkintrigg Posted January 5, 2008 Share Posted January 5, 2008 Call me weird, but i think that str_replace() would probably serve you better. Link to comment https://forums.phpfreaks.com/topic/84606-how-to-manipulate-some-variables-inside-preg_replace-code/#findComment-431154 Share on other sites More sharing options...
adrianTNT Posted January 5, 2008 Author Share Posted January 5, 2008 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? Link to comment https://forums.phpfreaks.com/topic/84606-how-to-manipulate-some-variables-inside-preg_replace-code/#findComment-431439 Share on other sites More sharing options...
acidglitter Posted January 5, 2008 Share Posted January 5, 2008 what do you mean by truncate the \\2? and i also think that str_replace() would work better because you're always looking for http:// or www. those don't change so you don't really need regular expressions. Link to comment https://forums.phpfreaks.com/topic/84606-how-to-manipulate-some-variables-inside-preg_replace-code/#findComment-431444 Share on other sites More sharing options...
adrianTNT Posted January 5, 2008 Author Share Posted January 5, 2008 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. Link to comment https://forums.phpfreaks.com/topic/84606-how-to-manipulate-some-variables-inside-preg_replace-code/#findComment-431449 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.