madjack87 Posted December 30, 2013 Share Posted December 30, 2013 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>] Quote Link to comment https://forums.phpfreaks.com/topic/284975-how-to-change-all-content-between-set-of-brackets/ Share on other sites More sharing options...
jcbones Posted December 30, 2013 Share Posted December 30, 2013 preg_match() is not the droid you are looking for. preg_match_all() is what you are looking for. Quote Link to comment https://forums.phpfreaks.com/topic/284975-how-to-change-all-content-between-set-of-brackets/#findComment-1463293 Share on other sites More sharing options...
Solution Ch0cu3r Posted December 30, 2013 Solution Share Posted December 30, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/284975-how-to-change-all-content-between-set-of-brackets/#findComment-1463298 Share on other sites More sharing options...
madjack87 Posted December 31, 2013 Author Share Posted December 31, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/284975-how-to-change-all-content-between-set-of-brackets/#findComment-1463417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.