Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Regex如何在第一次出现之前获取数据?_Php_Regex - Fatal编程技术网

Php Regex如何在第一次出现之前获取数据?

Php Regex如何在第一次出现之前获取数据?,php,regex,Php,Regex,我现在使用的表达式是(我会使用类似于: (?:<div class="oembed|\G)(?:(?!</div>).)*?\KV (?:Sheesh,我不得不使html警告静音,并跳过一系列utf-8环,以使DOM解析器技术给出正确的结果,但接下来……我确实对您的示例html进行了一些调整,并将其全部包装在父div中以保持稳定性。我认为这是可以做到的,因为您的示例字符串看起来像实际文档的一个片段t 我的XPath表达式将在文档中的任何位置搜索包含类的,然后搜索其文本以确保它

我现在使用的表达式是
(我会使用类似于:

(?:<div class="oembed|\G)(?:(?!</div>).)*?\KV

(?:Sheesh,我不得不使html警告静音,并跳过一系列utf-8环,以使DOM解析器技术给出正确的结果,但接下来……我确实对您的示例html进行了一些调整,并将其全部包装在父div中以保持稳定性。我认为这是可以做到的,因为您的示例字符串看起来像实际文档的一个片段t

我的XPath表达式将在文档中的任何位置搜索包含类的
,然后搜索其文本以确保它包含目标子字符串(
V
)。如果它找到一个符合条件的节点,foreach的主体将看到子字符串按需被替换

只要您的文档可以被解析,它将是一个更准确/可靠的解决方案,更不用说与不懂DOM的工具regex相比更易于维护

代码:()


@ChrisRuehlemann感谢您的评论,但仍然是相同的结果我们的要求不清楚。请详细解释。这将有助于我们更好地理解问题所在。@Mandy8055谢谢您的帮助,如果html是动态的,我确实更新了我的问题;我建议您使用解析器,因为正则表达式可能会适得其反。否则,我建议您使用帮助?为什么不使用解析器?HTML和regex不是好朋友。使用解析器,它更简单、更快、更易于维护。非常感谢您的帮助,它工作得非常好。我仍然很好奇为什么这个表达式不工作:)再次,谢谢you@whitesiroi:因为
(
'   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>'
(?:<div class="oembed|\G)(?:(?!</div>).)*?\KV
$res = preg_replace('~(?:<div class="oembed|\G)(?:(?!</div>).)*?\KV~', '@CHANGE@', $text);
$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);
<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&amp;format=json&amp;maxwidth=500&amp;width=1" data-oembed-id="B5mtrL3p3XV" data-oembed-options="{&quot;maxwidth&quot;:&quot;500&quot;,&quot;width&quot;: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>