>  기사  >  백엔드 개발  >  正则表达式 - 求助怎么写php的正则匹配

正则表达式 - 求助怎么写php的正则匹配

WBOY
WBOY원래의
2016-06-06 20:32:411048검색

我要取出 字符串"user=asdasd; token=dwwewee; type=assdfs" 里的token值dwwewee,
php怎么写?
真的不会写。。

这样好像也不行

<code>preg_match("/(token=)(.*?)(;|$)/i","user=asdasd; token=dwwewee; type=assdfs", $matches);
foreach ($matches as $m)
{
    echo $m;echo "<br>";
}
</code>

回复内容:

我要取出 字符串"user=asdasd; token=dwwewee; type=assdfs" 里的token值dwwewee,
php怎么写?
真的不会写。。

这样好像也不行

<code>preg_match("/(token=)(.*?)(;|$)/i","user=asdasd; token=dwwewee; type=assdfs", $matches);
foreach ($matches as $m)
{
    echo $m;echo "<br>";
}
</code>

<code>(?</code>

<code>if (preg_match("/(token=)(.*?)(;|$)/i","user=asdasd; token=dwwewee; type=assdfs", $matches)) {
    print_r($matches[2]);
}
</code>

如果匹配成功,那么会把结果存放在$matches这个array中,index 0 是匹配的完整字符串,1 是你第一个括号, 2 是第二个括号,依次类推。从你的需求看,你需要的值是出现在第二个括号,所以取值 $matches[2]

<code>php</code><code>//php5.6 echo explode('=',explode(';',$str)[1])[1];//dwwewee
$arr = explode(';',"user=asdasd; token=dwwewee; type=assdfs");
$arr2 = explode('=',$arr[1]);
echo $arr2[1];//dwwewee
</code>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.