Home > Backend Development > PHP Tutorial > 正规表达式匹配的有关问题

正规表达式匹配的有关问题

WBOY
Release: 2016-06-13 12:52:45
Original
830 people have browsed it

正规表达式匹配的问题

本帖最后由 ConRst 于 2013-03-04 16:19:37 编辑 字符串是这样的
&AAA=111&BBB=222&CCC=333...
现在想用正规匹配得到AAA和111这样的值

<br />
<br />
$RegStr='/&(.*)=(.*)/';  //请问这个表达式是不是有问题  我这里的输出结果不对<br />
if(preg_match($RegStr,$urladd,$mat))<br />
{<br />
	print_r($mat);   //这里如何输出 AAA和 111 这样的两个变量的值<br />
<br />
}<br />
Copy after login


上面代码输出有问题,贴出来是让大家看明白,请大家帮忙


------解决方案--------------------
$RegStr='/&(.*?)=([^&]+)/';
------解决方案--------------------
<br />
$str='&AAA=111&BBB=222&CCC=333';<br />
preg_match_all('/&(\w+)=(\w+)/i',$str,$result);<br />
var_dump($result);<br />
Copy after login


array(3) {
  [0]=>
  array(3) {
    [0]=>
    string(8) "&AAA=111"
    [1]=>
    string(8) "&BBB=222"
    [2]=>
    string(8) "&CCC=333"
  }
  [1]=>
  array(3) {
    [0]=>
    string(3) "AAA"
    [1]=>
    string(3) "BBB"
    [2]=>
    string(3) "CCC"
  }
  [2]=>
  array(3) {
    [0]=>
    string(3) "111"
    [1]=>
    string(3) "222"
    [2]=>
    string(3) "333"
  }
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template