【发布时间】:2013-01-06 08:12:57
【问题描述】:
我似乎无法在需要使用正则表达式时正确使用它们...
给定一个这样的字符串:
$string = 'text here [download="PDC" type="A"] and the text continues [download="PDS" type="B"] and more text yet again, more shotcodes might exist ...';
我需要打印“text here”部分,然后根据变量“PDC”和“A”执行mysql查询,然后打印字符串的其余部分......(如果更多,请再次重复[下载]存在于字符串中)。
到目前为止,我有以下正则表达式
$regex = '/(.*?)[download="(.*?)" type="(.*?)"](.*?)/';
preg_match($regex,$string,$res);
print_r($res);
但这只是捕获以下内容:
Array ( [0] => 111111 [1] => 111111 [2] => )
我正在使用 preg_match() ... 我应该使用 preg_match_all() 代替吗?无论如何......正则表达式肯定是错误的......有什么帮助吗?
【问题讨论】:
-
[打开字符类,]完成它。此类有意义的字符需要转义或放入 PCRE 正则表达式中的 QE 块中。 -
对不起。我没有关注你...
-
应该是 $regex = '/(.*?)[download="(.*?)" tipo="(.*?)"](.*?)/'; ?
-
当我翻译这个问题时有错别字......应该读作“类型”
-
是的,说西班牙语的人应该知道错字“tipo”;)