0

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&amp;b=5&amp;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

2 Answers 2

1

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&amp;b=5&amp;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)

Sign up to request clarification or add additional context in comments.

2 Comments

That was not the problem. That works with your solution too, after i checked, and work with this also: @<div class=\"vmargin\".*?</div>.*?</div></div>@s without any escaping (at the divs).
my regex didn't work but yours did.Can you explain why mine don't work since you say `\` was not the problem
1

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&amp;b=5&amp;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&amp;b=5&amp;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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.