php - 正则表达式如何获取数据直到第一次出现?
问题描述
我现在的表情(<div class=\"oembed.*)V(.*?)<\/div>
如何获取数据直到 1 日"</div>"
?因为现在它一直持续到最后 3 日"</div>"
更新#1:
我正在寻找V
以class="oembed
我想用正则表达式得到的结果:
<div class="oembed oembed-type-instagram oembed-pre-frame" data-oembed-medialink="https://www.instagram.com/p/B_BEXwtp-V7/" style="margin:10px auto;" data-oembed-url="https://api.instagram.com/oembed/?url=https%3A%2F%2Fwww.instagram.com%2Fp%2FB_BEXwtp-V7&format=json&maxwidth=500&embed=widget&width=1" data-oembed-id="B_BEXwtp-V7" data-oembed-options='{"maxwidth":"500","embed":"widget","width":true}'>1st div</div>
更新#2:
我正在使用这个 PHP 代码: https ://paiza.io/projects/aU_FO4ihErlQngYFy6xvJg
结果是:
' Vがある<br>
<div id="body-top" class="content-moki clearfix">
Vがある<br>
<span class="headline">
<br>
にモデル「V」を入れると、プレビューでエンベットが崩れる<br>
<br>
<div class="oembed oembed-type-instagram" data-oembed-medialink="https://www.instagram.com/p/B5mtrL3p3X@CHANGE@/" style="margin:10px auto;max-width: 500px;" data-oembed-url="https://api.instagram.com/oembed/?url=https%3A%2F%2Fwww.instagram.com%2Fp%2FB5mtrL3p3X@CHANGE@&format=json&maxwidth=500&width=1" data-oembed-id="B5mtrL3p3X@CHANGE@" data-oembed-options=\'{"maxwidth":"500","width":true}\'><figure class="moki-embed-instagram"><img src="https://instagram.com/p/B5mtrL3p3X@CHANGE@/media/?size=l"><figcaption><i class="fa fa-instagram icon"></i></figcaption></figure></div> @CHANGE@がある<br>a </div> @CHANGE@...</div>'
但我必须得到这个结果:
' Vがある<br>
<div id="body-top" class="content-moki clearfix">
Vがある<br>
<span class="headline">
<br>
にモデル「V」を入れると、プレビューでエンベットが崩れる<br>
<br>
<div class="oembed oembed-type-instagram" data-oembed-medialink="https://www.instagram.com/p/B5mtrL3p3X@CHANGE@/" style="margin:10px auto;max-width: 500px;" data-oembed-url="https://api.instagram.com/oembed/?url=https%3A%2F%2Fwww.instagram.com%2Fp%2FB5mtrL3p3X@CHANGE@&format=json&maxwidth=500&width=1" data-oembed-id="B5mtrL3p3X@CHANGE@" data-oembed-options=\'{"maxwidth":"500","width":true}\'><figure class="moki-embed-instagram"><img src="https://instagram.com/p/B5mtrL3p3X@CHANGE@/media/?size=l"><figcaption><i class="fa fa-instagram icon"></i></figcaption></figure></div> Vがある<br>a </div> V...</div>'
你可以在这里看到区别 - https://www.diffchecker.com/n4LIOMtH
解决方案
哎呀,我不得不使 html 警告静音并跳过一堆 utf-8 箍来让这种 DOM 解析器技术吐出正确的结果,但是这里......我确实稍微调整了你的示例 html 并将其全部包含在用于稳定性的父 div。我认为这是可以的,因为您的示例字符串看起来像实际文档的片段。
我的 XPath 表达式将在文档中的任何位置搜索<div>
包含该类的类oembed
,然后搜索其文本以确保它包含目标子字符串 ( V
)。如果找到符合条件的节点,则 foreach 的主体将看到子字符串已根据需要替换。
只要您的文档可以被解析,它将是一个更准确/可靠的解决方案,更不用说与正则表达式相比更容易维护 - 这是一个不了解 DOM 的工具。
代码:(演示)
$html = <<<HTML
<div>
Vがある<br>
<div id="body-top" class="content-moki clearfix">
Vがある<br>
<span class="headline">
<br>
にモデル「V」を入れると、プレビューでエンベットが崩れる<br>
<br>
<div class="oembed oembed-type-instagram" data-oembed-medialink="https://www.instagram.com/p/B5mtrL3p3XV/" style="margin:10px auto;max-width: 500px;" data-oembed-url="https://api.instagram.com/oembed/?url=https%3A%2F%2Fwww.instagram.com%2Fp%2FB5mtrL3p3XV&format=json&maxwidth=500&width=1" data-oembed-id="B5mtrL3p3XV" data-oembed-options='{"maxwidth":"500","width":true}'>
<figure class="moki-embed-instagram">
<img src="https://instagram.com/p/B5mtrL3p3XV/media/?size=l">
<figcaption>
<i class="fa fa-instagram icon"></i>
</figcaption>
</figure>
Vがある
</div>
<br>a
</span>
V...
</div>
</div>
HTML;
$needle = 'V';
$replace = '@CHANGE@';
libxml_use_internal_errors(true);
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
foreach ($xpath->query("//div[contains(@class, 'oembed')]/text()[contains(.,'$needle')]") as $node) {
$node->nodeValue = str_replace($needle, $replace, $node->nodeValue);
}
echo $dom->saveXML($dom->documentElement);
输出:(注意只有V
存在于目标 div 中的 被替换)
<div>
Vがある<br/>
<div id="body-top" class="content-moki clearfix">
Vがある<br/>
<span class="headline">
<br/>
にモデル「V」を入れると、プレビューでエンベットが崩れる<br/>
<br/>
<div class="oembed oembed-type-instagram" data-oembed-medialink="https://www.instagram.com/p/B5mtrL3p3XV/" style="margin:10px auto;max-width: 500px;" data-oembed-url="https://api.instagram.com/oembed/?url=https%3A%2F%2Fwww.instagram.com%2Fp%2FB5mtrL3p3XV&format=json&maxwidth=500&width=1" data-oembed-id="B5mtrL3p3XV" data-oembed-options="{"maxwidth":"500","width":true}">
<figure class="moki-embed-instagram">
<img src="https://instagram.com/p/B5mtrL3p3XV/media/?size=l"/>
<figcaption>
<i class="fa fa-instagram icon"/>
</figcaption>
</figure>
@CHANGE@がある
</div>
<br/>a
</span>
V...
</div>
</div>