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
https://forums.phpfreaks.com/topic/208580-regular-expression-php-help-please/
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;

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.