如何应用此正则表达式?在 PHP 中



如何在Php中应用此正则表达式?我有代码

$a = "{temp}name_temp1{/temp}  Any thing Any thing {temp}name_temp2{/temp}";

我只需要name_temp1name_temp2

{temp}

This{/temp} 中的任何名称

谢谢

您可以使用惰性量词:

{temp}         # look for {temp}
(?P<value>.+?) # anything else afterwards
{/temp}        # look for {/temp}


PHP中,这将是:
<?php
$a = "{temp}name_temp1{/temp}  Any thing Any thing {temp}name_temp2{/temp}";
$regex = '~{temp}(?P<value>.+?){/temp}~';
preg_match_all($regex, $a, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
    echo $match["value"];
}
?>

试试这个正则表达式: {temp}(.*?){/temp}

你可以在PHP中像这样使用它:

$a = "{temp}name_temp1{/temp}  Any thing Any thing {temp}name_temp2{/temp}";
preg_match_all('/{temp}(.*?){/temp}/', $a, $matches);
var_dump($matches[1]); // Returns ['name_temp1', 'name_temp2']

eval.in 演示

最新更新