dde Posted January 20, 2015 Share Posted January 20, 2015 (edited) I'm looking for a method to replace everything between brackets.The patterns that I'm looking for are defined: "/\[([2-9TJQKA][hdcs])\]/"The pattern inside the bracket could accur 2 or even 3 times inside the brackets.For example first occurance would be [Ac Kd], here I want to replace Ac into an imge and Kd too.Later in the text I will most of the time get 3 sets, instead of two: [Jc Td 9d]. The sets always consist of [2-9JQKA][hdcs] and also in this order.Another example: [2d 2s 2h], or [3s 4h 5c].If I use the following function, it would not work optimally because some texts do contain some of those sets which should not be converted, ONLY the sets between the brackets: $haystack = setReplace($haystack); function setReplace($haystack) { $array = array("2s", "2h", "2d", "2c", "3s", "3h", "3d", "3c", "4s", "4h", "4d", "4c", "5s", "5h", "5d", "5c", "6s", "6h", "6d", "6c", "7s", "7h", "7d", "7c", "8s", "8h", "8d", "8c", "9s", "9h", "9d", "9c", "Ts", "Th", "Td", "Tc", "Js", "Jh", "Jd", "Jc", "Qs", "Qh", "Qd", "Qc", "Ks", "Kh", "Kd", "Kc", "As", "Ah", "Ad", "Ac"); for($i=0;$i < count($array);$i++) { $result = str_replace($array[$i], "<img src=\"/images/".$code[$i].".png\">",$haystack); } return $result; } Any ideas? Edited January 20, 2015 by dde Quote Link to comment https://forums.phpfreaks.com/topic/294083-preg_replace-one-or-more-sets-of-letters-between-brackets/ Share on other sites More sharing options...
dde Posted January 20, 2015 Author Share Posted January 20, 2015 (edited) I have also tried the following, but not sure what to do with the result. preg_match_all("/\[(.*?)\]/", $text, $matches); $array = array(); for($i=0; $i < count($matches[1]); $i++){ $split = explode(" ", $matches[1][$i]); foreach ($split as $value){ array_push($array, $value); } } Edited January 20, 2015 by dde Quote Link to comment https://forums.phpfreaks.com/topic/294083-preg_replace-one-or-more-sets-of-letters-between-brackets/#findComment-1503514 Share on other sites More sharing options...
Solution Ch0cu3r Posted January 20, 2015 Solution Share Posted January 20, 2015 Try $text = 'Another example: [2d 2s 2h], or [3s 4h 5c]'; // find the codes within the square brackets // pass the matches to a callback function to replace the codes with images $text = preg_replace_callback('/\[(([2-9TJQKA][hdcs]\s?)+)\]/', function($m) { // separate the codes between spaces $codes = explode(' ', $m[1]); $html = ''; // loop over each code and replace it with the corresonding an image foreach ($codes as $code) { $html .= '<img src="/images/'.$code.'.png">'; } // return the images return $html; }, $text); echo $text; Quote Link to comment https://forums.phpfreaks.com/topic/294083-preg_replace-one-or-more-sets-of-letters-between-brackets/#findComment-1503527 Share on other sites More sharing options...
dde Posted January 20, 2015 Author Share Posted January 20, 2015 Ch0cu3r, could you explain me what the question mark and the plus sign is for? php.net manual says:? extends the meaning of (, also 0 or 1 quantifier, also makes greedy quantifiers lazy.+1 or more quantifier.I'm lost. Quote Link to comment https://forums.phpfreaks.com/topic/294083-preg_replace-one-or-more-sets-of-letters-between-brackets/#findComment-1503555 Share on other sites More sharing options...
Ch0cu3r Posted January 20, 2015 Share Posted January 20, 2015 The regex ([2-9TJQKA][hdcs]\s?)+ is finding the two character codes within the square brackets The reason for the ? is to match whether or not a (white)space character comes after the two character code. As each code is separated by a space, except the last code. The + is so it matches all the two character codes followed by the optional space character within the square brackets. Without it, only the first two character code will be found. See the differences yourself by adding and removing the ? or + from the regex pattern here http://regexr.com/3a8gu 1 Quote Link to comment https://forums.phpfreaks.com/topic/294083-preg_replace-one-or-more-sets-of-letters-between-brackets/#findComment-1503566 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.