Jump to content

preg_replace one or more sets of letters between brackets


dde
Go to solution Solved by Ch0cu3r,

Recommended Posts

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 by dde
Link to comment
Share on other sites

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 by dde
Link to comment
Share on other sites

  • Solution

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;
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.