改进我的正则表达式+php 替换

Improve my regex+php replacement

我正在尝试用正则表达式替换字符串的一部分。我的代码可以完成这项工作,但这是正确的方法吗?

$string = 'blabla <!-- s:D --><img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /><!-- s:D --> blabla <!-- scat --><img src="{SMILIES_PATH}/cat2.gif" alt="cat" title="Cat" /><!-- scat --> blabla';
$pattern = '(<!-- s(\S*) --><img src="\S*" alt="\S*" title="[^"]+" \/><!-- s\S* -->)';

preg_match_all($pattern, $string, $result);

$i = 0;
foreach ($result[0] as $match) {
    $string = str_replace($match, $result[1][$i], $string);
    $i++;
}

我想要的:blabla :D blabla cat blabla

正则表达式测试:https://regex101.com/r/fD0xI2/2

PHP 测试:http://ideone.com/mrS0BJ

我猜你可以减小正则表达式的大小,即:

$string = 'blabla <!-- s:D --><img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /><!-- s:D --> blabla <!-- scat --><img src="{SMILIES_PATH}/cat2.gif" alt="cat" title="Cat" /><!-- scat --> blabla';

preg_match_all('/(\S+) <!-- (.*?) -->/sm', $string , $matches, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($matches[1]); $i++) {
    $newString .= $matches[1][$i] ." ".$matches[2][$i]." " ;
}

echo $newString;

输出:

blabla s:D blabla scat 

演示:

http://ideone.com/9fons0


正则表达式扩展:

(\S+) <!-- (.*?) -->

Options: Dot matches line breaks; ^$ match at line breaks; Greedy quantifiers

Match the regex below and capture its match into backreference number 1 «(\S+)»
   Match a single character that is NOT a “whitespace character” «\S+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character string “ <!-- ” literally « <!-- »
Match the regex below and capture its match into backreference number 2 «(.*?)»
   Match any single character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “ -->” literally « -->»