【发布时间】:2015-12-20 22:19:55
【问题描述】:
在 WordPress/PHP 安装中,我需要从 Google 提供的文字广告中检索由大小写字母、数字和连字符(即 11 位 YouTube 视频 ID)组成的数据值DFP 广告服务器。输出示例如下。在示例中,我有以下描述的数据值位置:
I-NEED-THIS-1: This data value is optional and available only when I add it to the ad in Google DFP.
I-NEED-THIS-2: This data value is required when I create the ad in Google DFP and is the most reliable to always be available.
I-NEED-THIS-3: This data value is optional and available only when I add it to the ad in Google DFP.
Google DFP 输出示例:
<div class="a-single a-x">
<div id="div-gpt-ad-xxxxxxxxxxxxx-0">
<script>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-xxxxxxxxxxxxx-0'); });
</script>
<div id="google_ads_iframe_/xxxxxxxx/AD-NAME_0__container__" style="border: 0pt none;">
<iframe id="google_ads_iframe_/xxxxxxxx/AD-NAME_0">
</iframe>
</div>
#document
<html>
<head>
<script>var xxxxxxx=true;</script>
</head>
<body marginwidth="0" marginheight="0">
<a href="https://adclick.g.doubleclick.net/aclk....." title="I-NEED-THIS-1" target="_blank">
<span style="color:cccccc">
<b>I-NEED-THIS-2</b>
</span>
</a>
<span style="color:black">I-NEED-THIS-3</span>
<br>
<script type="text/javascript"></script>
</body>
</html>
</div>
</div>
我尝试过使用 here 描述的 preg_match 表达式的一个版本:
preg_match("'<span style=\"color:cccccc\"><b>(.*?)</b></span>'si", $source, $match);
结果输出'...googletag.cmd.push(function()...'脚本直到'}'。似乎不想越过这个脚本并定位模式。
我尝试了其他变体,具有不同的模式,例如:
'#<a.+?title="([a-zA-Z0-9_-]{11})[^"]*"[^>]+?>[\S\s]+?</a>#i'
"'<div><div><script><div><iframe><html><body><a><span><b>(.*?)</b></span></a></body></html></iframe></div></script></div></div>'si"
结果不符合我正在寻找的目标。
如何检索此数据值并使其在变量中可用? preg_match 是 WordPress / PHP 安装的最佳方法吗?
感谢所有帮助!
【问题讨论】:
-
你做得很辛苦。查看 html 解析器。
-
XPath via
DOMDocument会更好:stackoverflow.com/questions/12547356/…
标签: php regex wordpress parsing preg-match