[英]PHP regex preg_match_all for string
我有以下字符串:
关键字|标题| HTTP://example.com/
我想使用PHP函数
preg_match_all ( $anchor, $key, $matches, PREG_SET_ORDER) )
目前
$anchor='/([\w\W]*?)\|([\w\W]*)/';
我得到$ matches数组:
Array
(
[0] => Array
(
[0] => keyword|title|http://example.com/
[1] => keyword
[2] => title|http://example.com/
)
)
我想得到
matches[1]=keyword
matches[2]=title
matches[3]=http://example.com
我如何修改$ anchor才能实现此目的?
如果您想继续使用正则表达式来避免手动循环,那么建议您在使用的[\\w\\W]*
语法上使用此正则表达式,以提高可读性:
$anchor = '/([^|]*) \| ([^|]*) \| ([^\s|]+)/x';
显式否定字符类的功能更强大。 (我假设标题和url都不能包含|
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.