Jump to content

How to change all content between set of brackets.


madjack87

Recommended Posts

What I am doing is displaying a message to another user on the company system.

 

This is Test 1 [20345]

 

The above data is what is stored in the database.

 

What I want to acheive is when I echo the field for it to do this:

 

This is Test 1 [<a href="http://mywebsite.com/system/page1.php?number=20345">20345</a>]

 

I currently am able to make this work. However I cannot get it to work on multiple links.

$message_message = $row['message_message'];	
      preg_match("/\[(.*)\]/", $message_message , $matches);
      $match = $matches[1]; 
      $parts = explode ("[", $message_message);
      $part1 = $parts[0];
      $part2 = $parts[1];
      $subparts = explode ("]", $part2);
      $part3 = $subparts[1];
    
      if ($match != ""){
      $new_message_text2 = $part1 . "[<a href=\"http://mywebsite.com/system/page1.php?number=$match\">$match</a>]" . $part3;
      }else{
        $new_message_text2 = $message_message;
      }

When I have data like this:

This is Test 2 [20345] [20552]

 

I get a result like this:

This is Test 2 [<a href="http://mywebsitecom/system/page1.php?number=20345] [20552">20345] [20552</a>]

 

 

Worked Like a Charm!

 

Also cleaned my code up a lot!

 

JCBones I will look into preg_replace_all for future use. Thank you.

 

 

Your could use preg_replace instead

$message_message = $row['message_message'];	

$message_message = preg_replace("/\[(\d+)\]/", '[<a href=\"http://mywebsite.com/system/page1.php?number=$1">$1</a>]', $message_message);

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.