当前位置:Gxlcms > PHP教程 > 取子串的正则表达式,该怎么处理

取子串的正则表达式,该怎么处理

时间:2021-07-01 10:21:17 帮助过:25人阅读

取子串的正则表达式
有如下字符串:
"name1name2"
用正则表达式如何取出"name1"和"name2"呢?谢谢

------解决方案--------------------
PHP code
name1name2";
$patten  = "/([^<]*)<\/str\d+>/isU";
preg_match_all($patten,$str,$matches);
print_r($matches[1]);

------解决方案--------------------
PHP code

$str = "name1name2";
preg_match_all('/>([^<]+)输出结果:
Array ( [0] => name1 [1] => name2 ) 
*/

------解决方案--------------------
PHP code
preg_match_all("/>[^<]+------解决方案--------------------
$str = "name1name2";
preg_match_all('/>([^<]+)print_r($matches[1]);
/**
输出结果:
Array ( [0] => name1 [1] => name2 )
*/

------解决方案--------------------
PHP code

name1name2";
    $preg="#(.*)(.*)#";
    preg_match($preg,$str,$arr);
    echo $arr[1];
    echo $arr[2];
?>

------解决方案--------------------
$str="name1name2";
preg_match_all("/<[a-z0-9]*>([^<|>]*)<\/[a-z0-9]*>/",$str,$matches);
echo $matches[1][0];
echo $matches[1][1];
?>

------解决方案--------------------
"/>(.*?)<\//"
------解决方案--------------------
探讨

PHP code

$str = "name1name2";
preg_match_all('/>([^<]+)print_r($matches[1]);
/**
输出结果:
Array ( [0] => name1 [1] => name2 )
*/

------解决方案--------------------
$str="name1name2";
preg_match_all("/<[a-z0-9]*>([^<|>]*)<\/[a-z0-9]*>/",$str,$matches);
echo $matches[1][0];
echo $matches[1][1];
?>

人气教程排行