失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > php正则匹配怎么写 正则表达式 - 求助怎么写php的正则匹配

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

时间:2021-02-06 12:13:10

相关推荐

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

我要取出 字符串"user=asdasd; token=dwwewee; type=assdfs" 里的token值dwwewee,

php怎么写?

真的不会写。。

这样好像也不行

preg_match("/(token=)(.*?)(;|$)/i","user=asdasd; token=dwwewee; type=assdfs", $matches);

foreach ($matches as $m)

{

echo $m;echo "

";

}

回复内容:

我要取出 字符串"user=asdasd; token=dwwewee; type=assdfs" 里的token值dwwewee,

php怎么写?

真的不会写。。

这样好像也不行

preg_match("/(token=)(.*?)(;|$)/i","user=asdasd; token=dwwewee; type=assdfs", $matches);

foreach ($matches as $m)

{

echo $m;echo "

";

}

(?<=token=).*(?=;)

if (preg_match("/(token=)(.*?)(;|$)/i","user=asdasd; token=dwwewee; type=assdfs", $matches)) {

print_r($matches[2]);

}

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

php//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

发布php中文网,转载请注明出处,感谢您的尊重!

如果觉得《php正则匹配怎么写 正则表达式 - 求助怎么写php的正则匹配》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。