PHP - Benim REGEX tabanlı özyinelemeli fonksiyonu ile yardım

2 Cevap php

I'm extracting a string from wikipedia API that initially looks like this: link text. I want to peel off all {{...}} and everything in between them (could be any kind of text). For that I thought about using a recursive function with "preg_match","preg_replace". something like:

function drop_brax($text)
{
    if(preg_match('/{{(.)*}}/',$text)) 
    return drop_brax(preg_replace('/{{(.)*}}/','',$text));
    return $text;
}

Bu fonksiyon, çünkü bu gibi bir durumun çalışmaz:

{{ I like mocachino {{ but I also like banana}} and frutis }}

this will peel off everything between the first occurence of both {{ and }} (and leave out "and frutis }}"). How can I do this properly? (while maintaining the nice recursive form).

2 Cevap

Böyle bir şey deneyin:

$text = '...{{aa{{bb}}cc}}...{{aa{{bb{{cc}}bb{{cc}}bb}}dd}}...';
preg_match_all('/\{\{(?:[^{}]|(?R))*}}/', $text, $matches);
print_r($matches);

çıktı:

Array
(
    [0] => Array
        (
            [0] => {{aa{{bb}}cc}}
            [1] => {{aa{{bb{{cc}}bb{{cc}}bb}}dd}}
        )
)

Ve kısa bir açıklama:

\{\{      # match two opening brackets
(?:       # start non-capturing group 1
  [^{}]   #   match any character except '{' and '}'
  |       #   OR
  (?R)    #   recursively call the entire pattern: \{\{(?:[^{}]|(?R))*}}
)         # end non-capturing group 1
*         # repeat non-capturing group 1 zero or more times
}}        # match two closing brackets

Bu tamamen özyinelemeli olması için bir ayrıştırıcı ihtiyacınız olacak:

function drop_brax($str)
{
	$buffer = NULL;
	$depth = 0;
	$strlen_str = strlen($str);
	for($i = 0; $i < $strlen_str; $i++)
	{
		$char = $str[$i];

		switch ($char)
		{
			case '{':
				$depth++;
			break;
			case '}':
				$depth--;
			break;
			default:
				$buffer .= ($depth === 0) ? $char : NULL;
		}
	}
	return $buffer;
}

$str = 'some text {{ I like mocachino {{ but I also like banana}} and frutis }} some text';
$str = drop_brax($str);
echo $str;

çıktı:

some text some text