取子串的正则表达式
有如下字符串:
"
用正则表达式如何取出"name1"和"name2"呢?谢谢
------解决方案--------------------
- PHP code
<?php $str = "<str1>name1</str1><str2>name2</str2>"; $patten = "/<str\d+>([^<]*)<\/str\d+>/isU"; preg_match_all($patten,$str,$matches); print_r($matches[1]); <br><font color="#e78608">------解决方案--------------------</font><br>
- PHP code
$str = "<str1>name1</str1><str2>name2</str2>";
preg_match_all('/>([^<]+)</U', $str, $matches);
print_r($matches[1]);
/**
输出结果:
Array ( [0] => name1 [1] => name2 )
*/
<br><font color="#e78608">------解决方案--------------------</font><br>- PHP code
preg_match_all("/>[^<]+</",$str,$m);
print_R($m[1]);
<br><font color="#e78608">------解决方案--------------------</font><br>$str = "<str1>name1</str1><str2>name2</str2>";<br>preg_match_all('/>([^<]+)</U', $str, $matches);<br>print_r($matches[1]);<br>/**<br>输出结果:<br>Array ( [0] => name1 [1] => name2 ) <br>*/<br>
<br><font color="#e78608">------解决方案--------------------</font><br>- PHP code
<?php
$str="<str1>name1</str1><str2>name2</str2>";
$preg="#<str1>(.*)</str1><str2>(.*)</str2>#";
preg_match($preg,$str,$arr);
echo $arr[1];
echo $arr[2];
?>
<br><font color="#e78608">------解决方案--------------------</font><br><?php<br> $str="<str1>name1</str1><str2>name2</str2>";<br> preg_match_all("/<[a-z0-9]*>([^<|>]*)<\/[a-z0-9]*>/",$str,$matches);<br> echo $matches[1][0];<br> echo $matches[1][1];<br>?><br>
<br><font color="#e78608">------解决方案--------------------</font><br>"/>(.*?)<\//"
<br><font color="#e78608">------解决方案--------------------</font><br>
------解决方案--------------------
$str="
preg_match_all("/([^]*)/",$str,$matches);
echo $matches[1][0];
echo $matches[1][1];
?>









