【问题标题】:How to find a string pattern in preg_match_all match array?如何在 preg_match_all 匹配数组中找到字符串模式?
【发布时间】:2016-01-11 15:12:04
【问题描述】:

我有一个带有 html 代码的字符串。它有很多网址格式。我使用 preg_match_all 来获取所有网址。现在我想循环遍历 $match2 数组,并且只打印在完整 url 中某些位置具有“.m3u8”的 url。 var_dump($match2) 正确打印所有网址,但我的 for 循环不断给我这个错误;

E_NOTICE : type 8 -- Array to string conversion 

谁能帮我解决这个问题?提前谢谢。

php代码:

$regex2 = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";

preg_match_all($regex2,$code,$match2);
var_dump($match2);

for($i = 0; $i < count($match2); $i++){

  if (strpos($match2[$i],'.m3u8') !== false) {
    echo "<br>".$match2[$i];
    } else {
       //do nothing
    }
 }

【问题讨论】:

  • var_dump($match2); 你已经做对了。查看它的输出,然后再考虑要循环遍历哪个数组。
  • 感谢我将 $match2 更改为 $match2[0] 并且它出错了!
  • 现在你甚至可以自己回答你的问题,去做吧。

标签: php arrays pattern-matching preg-match-all


【解决方案1】:

我将 $match2 更改为 $match2[0] 并且它出错了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2021-12-19
    • 2014-05-07
    • 2020-08-21
    相关资源
    最近更新 更多