Preg_replace multiple instances in 1 line?

I have such text from poker software (I highlighted the parts that I need to replace).

--- FLOP --- [Jh 9h Ah]
drivers for verification Darrington has 15 seconds to act
Darrington bids 100
InvisibleEnigma calls 100
folds of drivers

--- TURN --- [Jh 9h Ah] [3c]
Darrington bids 200
InvisibleEnigma calls 200
--- SPEECH --- [Jh 9h Ah 3c] [Td]

Jh = Jack of hearts
9h = 9 hearts
Ah = Ace of hearts
3c = 3 clubs

I want to replace cards inside the square brackets of the IMAGE .

, : --- TURN --- [Jh 9h Ah] [3c]
: --- TURN --- jh.gif 9h.gif ah.gif 3c.gif

preg_replace:( (, [3c]), 1 , 3 , 2 1 .

, :

\[([AKQJakqj1-9]0?[DHSCdhsc])\]

.

+3
3

, ( ). , , cinch:

\b([AKQJakqj1-9]0?[DHSCdhsc])\b

\b . , , Ah Ahead , .

. lookahead and lookbehind , , lookbehind, :

lookbehind , , , .

, , /e, preg_replace .

preg_replace(
    '/\[(.*?)]/e',
    "preg_replace('/(\b[AKQJT1-9]0?[DHSC]\b)/i', '\\\$1.jpg', '$1')",
    $hh
);

. $1 .

, T 10 diams; Td , i, .

+1
$string = "--- RIVER --- [Jh 9h Ah 3c] [Td]";

$CARD = "[akqjt1-9][dhsc]";
echo preg_replace(
  "/\[($CARD(?:\s+$CARD){0,4})\]/ie",
  "preg_replace('/\b$CARD\b/i', '\\\$0.gif', '\$1')",
  $string);

:

--- RIVER --- Jh.gif 9h.gif Ah.gif 3c.gif Td.gif


, (!) , , - , [as the world turns], . 5 , , {0,4} .
0
function imageize($matches)
{
    $from = array('s','c','d','h','[',']');
    $to = array('s.gif','c.gif','d.gif','h.gif','','');
    return $matches[1] . str_replace($from, $to, strtolower($matches[2])) . $matches[3];
}

$str = preg_replace_callback('#([^\[\]]*?)((?: *\[[akqjtcsdh\d ]+\])+)([^\[\]]*?)#i', 'imageize', $str);
0
source

Source: https://habr.com/ru/post/1762001/


All Articles