Home >Backend Development >PHP Tutorial > 取子串的正则表达式,该怎么处理

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

WBOY
WBOYOriginal
2016-06-13 13:49:30968browse

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

------解决方案--------------------

PHP code
<?php $str = "<str1>name1<str2>name2</str2>";
$patten  = "/<str>([^/isU";
preg_match_all($patten,$str,$matches);
print_r($matches[1]);
<br><font color="#e78608">------解决方案--------------------</font><br><dl class="code">PHP code<pre class="brush:php;toolbar:false">

$str = "<str1>name1</str1><str2>name2</str2>";
preg_match_all('/>([^ name1 [1] => name2 ) 
*/
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code
preg_match_all("/>[^<font color="#e78608">------解决方案--------------------</font><br>$str = "<str1>name1</str1><str2>name2</str2>";<br>preg_match_all('/>([^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<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("/([^]*)/",$str,$matches);<br>   echo $matches[1][0];<br>   echo $matches[1][1];<br>?><br>
<br><font color="#e78608">------解决方案--------------------</font><br>"/>(.*?)<font color="#e78608">------解决方案--------------------</font><br>
探讨

PHP code

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

------解决方案--------------------
$str="name1name2";
preg_match_all("/([^]*)/",$str,$matches);
echo $matches[1][0];
echo $matches[1][1];
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn