Jump to content

preg_replace one or more sets of letters between brackets


dde

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?

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);
   }

}

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;

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

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.