Shantam_Bhavsar Posted May 1, 2013 Share Posted May 1, 2013 <?php $data='123 [test=abc]cba[/test] 321'; $test = preg_replace("(\[test=(.+?)\](.+?)\[\/test\])is","$1",$data); echo $test; ?> I expect the above code to return abc but instead of returning abc it returns 123 abc 321 Please tell me where am i wrong. Quote Link to comment https://forums.phpfreaks.com/topic/277514-preg_replace-help/ Share on other sites More sharing options...
Solution DavidAM Posted May 1, 2013 Solution Share Posted May 1, 2013 You start with: 123 [test=abc]cba[/test] 321then you told it to replace [test=abc]cba[/test] with abc. that leaves 123 abc 321which, when printed to the browser will show as 123 abc 321 That is, the numbers (123 and 321) were not matched and therefore not affected by the preg_replace. Quote Link to comment https://forums.phpfreaks.com/topic/277514-preg_replace-help/#findComment-1427629 Share on other sites More sharing options...
Shantam_Bhavsar Posted May 1, 2013 Author Share Posted May 1, 2013 Thank you David, As per your explanation i changed line no. 5 to $test = preg_replace("(.*\[test=(.+?)\](.+?)\[\/test\].*)is","$1",$data); and it worked. Quote Link to comment https://forums.phpfreaks.com/topic/277514-preg_replace-help/#findComment-1427643 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.