gpt4 book ai didi

php - preg_match 返回奇怪的结果

转载 作者:行者123 更新时间:2023-12-02 01:22:35 26 4
gpt4 key购买 nike

我正在搜索字符串中的网址...而我的 preg_match 为我的演示字符串提供了不正确的匹配数量。

字符串:

Hey there, come check out my site at www.example.com

功能:

preg_match("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", $string, $links);
echo count($links);

结果为 3。

谁能帮我解决这个问题吗?我是正则表达式新手。

最佳答案

$linksarray of sub matches :

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

两组的匹配加上完整正则表达式的匹配会产生三个数组项。

也许您希望所有匹配都使用 preg_match_all .

关于php - preg_match 返回奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1773791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com