Jump to content

Regular Expression / PHP help please


Gooner

Recommended Posts

Hi

I have searched the forums for 2 hours and cannot find what I am looking for. Apologies if the answer is already here.

 

I need to write a php regular expression that will change any text within square brackets to a link.

 

Example

 

"Some text here. But look, a part ref number of [AK123] and another one just here [AK124]. that is all."

 

From this text, I need to add <a href> tags to the above, so that AK123 and AK124 link to http://www.mysite.com/ak123.php and http://www.mysite.com/ak123.php respectively. I also need the square brackets to go (the brackets are there to indicate the text that needs linking).

 

The result I need is:

 

"Some text here. But look, a part ref number of AK123 and another one just here AK124. that is all."

 

 

Can anyone help on this one?

 

 

Many Thanks

Link to comment
Share on other sites

$text = "Some text here. But look, a part ref number of [AK123] and another one just here [AK124]. that is all.";

$text = preg_replace('~\[([a-z0-9]+)\]~ie', '"<a href=\"http://site.com/".strtolower(\'\\1\').".php\">".strtoupper(\'\\1\')."</a>"', $text);

echo $text;

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.