我想从我正在提取的YouTube页面的源代码中挑选出一个值(在本例中,值为544007664)。目前,我拥有的脚本正在成功加载YouTube页面中的源代码,并删除了“<”和“>”符号,以便在回显源代码时显示为文本并且不显示页面本身。两个preg_replace()函数(这里是第4行和第5行)提取所需值前后的所有内容都没有达到我期望的效果。使用preg_replace()隔离源代码中的数字
我能想到的唯一的事情就是preg_replace()拒绝作为单个字符串文本阅读,而实际上是几十个单独的文本行。
<?php
$str = file_get_contents('https://www.youtube.com/watch?v=5XR7naZ_zZA');
$str = $str;
$str = preg_replace('~^(.)+(meta name="twitter:app:id:ipad" content=")~', '', $str);
$str = preg_replace('~(" meta name="twitter:app:url:iphone")+(.)$~', '', $str);
$str = preg_replace('~<~', '', $str);
$str = preg_replace('~>~', '', $str);
echo $str;
?>
任何帮助,非常感谢。
您可能想要使用'preg_match'。尝试模式[像这样](https://regex101.com/r/qI2nO3/2):'$ pattern ='/ ] +?twitter:app:id:iphone)[^>] +?\ bcontent \ s * = \ s *“([^”] +)“/ i';'结果'$ out [1]'包含你的第一个加括号的组的匹配,对'twitter:app:url:iphone'做同样的处理。 [见eval.in演示](https:// eval。in/505852)或者使用'preg_match_all'与单个正则表达式[如本示例](https://eval.in/505856)。 –