1
我试图提取给定字符串和第一个非字母数字字符之间的文本。下面的代码工作,但它使用标记而不是\ W。preg_match:字符串和第一个非字母数字字符
$my_string = 'Auth code: 02452A</div>';
preg_match("~Auth code:(.*)</div>~",$my_string, $m);
print_r($m);
// shouldn't this work, too?
preg_match("~Auth code:(.*)\W~",$my_string, $m);
不您指明@Wiktor的副本。你可以添加g标志:'preg_match(“〜Auth code:(。*)\ W〜g”,$ my_string,$ m);':参见https://stackoverflow.com/questions/12993629/g-flag-in-regular-expressions –
@PierreGranger:好的,可能不是一个骗局。不过,它是PHP而不是JS。 'g'修饰符不被支持,为了在PHP中获得多个匹配,你需要使用'preg_match_all'。我认为'preg_match(“〜Auth code:\ s *(。*?)\ W〜”,$ my_string,$ m);'会工作,但''〜验证码:\ s * \ K \ w +〜“ '好多了。请参阅[** IDEONE演示**](http://ideone.com/jVqRS5)。 –
@PierreGranger好吧,preg_match_all全球 – Banditvibe