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.

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.