php正则表达式无法在php中工作[preg_replace}
I am trying to remove the following html but my regex isn't working
<div class="vmargin"><div><iframe src="/test.php?u=N0Bhlant98C6MRj0D44HwJMuf5TdA%2F24oG9hQ2qqX6IR2IruUxVrrhLR4EpHQDvGtuHH4%2BLgJMBG6L5%2BTs6t6FfgCbo%3D&b=5&f=frame" style="width:718px;height:438px;border:0;margin:0;padding:0;" __idm_frm__="100"></iframe></div><div><a href="" class="report_video" data-video-id="732253" title="Report Video">Video Broken?</a></div></div>
I tried the following regex
preg_replace("@<div class=\"vmargin\".*?<\\/div>.*?<\\/div><\\/div>@s",'', $input);
What's wrong with it
我正在尝试删除以下html但我的正则表达式无效 p>
我尝试了以下正则表达式 p>
它出了什么问题 p>
div>&lt; div class =“vmargin”&gt;&lt; div&gt;&lt; iframe src =“/ test.php?u = N0Bhlant98C6MRj0D44HwJMuf5TdA%2F24oG9hQ2qqX6IR2IruUxVrrhLR4EpHQDvGtuHH4%2BLgJMBG6L5%2BTs6t6FfgCbo%3D&amp; amp; b = 5&amp; amp; amp; f = frame“style =”width:718px; height:438px; border:0; margin:0; padding:0;“ __idm_frm __ =“100”&gt;&lt; / iframe&gt;&lt; / div&gt;&lt; div&gt;&lt; a href =“”class =“report_video”data-video-id =“732253”title =“报告视频”&gt; 视频已损坏?&lt; / a&gt;&lt; / div&gt;&lt; / div&gt;
code> pre>
preg_replace(“@&lt; div class = \”vmargin \“。*?&lt; \\ / div&gt;。*?&lt; \\ / div&gt;&lt; \\ / div&gt; @s”,' ',$ input);
code> pre>
Do not use \\
because in your closing divs, there are no \
character. Try this:
<div class=\"vmargin\".*?<\/div>.*?<\/div><\/div>
So:
$string = '<div class="vmargin"><div><iframe src="/test.php?u=N0Bhlant98C6MRj0D44HwJMuf5TdA%2F24oG9hQ2qqX6IR2IruUxVrrhLR4EpHQDvGtuHH4%2BLgJMBG6L5%2BTs6t6FfgCbo%3D&b=5&f=frame" style="width:718px;height:438px;border:0;margin:0;padding:0;" __idm_frm__="100"></iframe></div><div><a href="" class="report_video" data-video-id="732253" title="Report Video">Video Broken?</a></div></div>';
$input = preg_replace("@<div class=\"vmargin\".*?<\/div>.*?<\/div><\/div>@s", '', $string);
var_dump($input);
Output: string '' (length=0)
I know it's not technically an answer, but to make the result readable (code formatting required)
Works for me:
<?php
$input = '<div class="vmargin"><div><iframe src="/test.php?u=N0Bhlant98C6MRj0D44HwJMuf5TdA%2F24oG9hQ2qqX6IR2IruUxVrrhLR4EpHQDvGtuHH4%2BLgJMBG6L5%2BTs6t6FfgCbo%3D&b=5&f=frame" style="width:718px;height:438px;border:0;margin:0;padding:0;" __idm_frm__="100"></iframe></div><div><a href="" class="report_video" data-video-id="732253" title="Report Video">Video Broken?</a></div></div>';
echo( "!".preg_replace("@<div class=\"vmargin\".*?<\\/div>.*?<\\/div><\\/div>@s",'', $input) .'!' );
Output:
C:\test>php test.php
!!
Moving to '
quoted strings and removing the escaping makes it easier to read though
$input = '<div class="vmargin"><div><iframe src="/test.php?u=N0Bhlant98C6MRj0D44HwJMuf5TdA%2F24oG9hQ2qqX6IR2IruUxVrrhLR4EpHQDvGtuHH4%2BLgJMBG6L5%2BTs6t6FfgCbo%3D&b=5&f=frame" style="width:718px;height:438px;border:0;margin:0;padding:0;" __idm_frm__="100"></iframe></div><div><a href="" class="report_video" data-video-id="732253" title="Report Video">Video Broken?</a></div></div>';
echo( '!'.preg_replace('@<div class="vmargin".*?</div>.*?</div></div>@s','', $input) .'!' );
Same output