Remove div from html content using PHP - php

I have an existing PHP app that generates this div element:
<div style='position:fixed;left:0;right:0;bottom:0;background:#f00;text-align:center'>
<div class='asdthjeme'>Designed by
<a href='http://blabla.com/' target='_blank'>dnfdjf</a></div></div>
I am trying to remove above div element from HTML content using PHP, but it doesn't work for me
My php code:
$text = '<div style='position:fixed;left:0;right:0;bottom:0;background:#f00;text-align:center'>
<div class='asdthjeme'>Designed by
<a href='http://blabla.com/' target='_blank'>dnfdjf</a></div></div>';
echo preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', $text);
Thanks in your help guys!

You can't use single quotes if your string is enclosed in single quotes.
Either escape them with \ or use different quotes.
$text = "<div style='position:fixed;left:0;right:0;bottom:0;background:#f00;text-align:center'>
<div class='asdthjeme'>Designed by
<a href='http://blabla.com/' target='_blank'>dnfdjf</a></div></div>";
or
$text = '<div style=\'position:fixed;left:0;right:0;bottom:0;background:#f00;text-align:center\'>
<div class=\'asdthjeme\'>Designed by
<a href=\'http://blabla.com/\' target=\'_blank\'>dnfdjf</a></div></div>';

Try this:
$text = '<div style="position:fixed;left:0;right:0;bottom:0;background:#f00;text-align:center">
<div class="asdthjeme">Designed by
dnfdjf</div></div>';

Related

Escape quotes on onclick metho

I have click method generated by php echo. It does not render as it should be. It shows as in the attached image.
my code is
echo "<div class='col-sm-3' onclick='ViewItem('".$item['item_id']."')' style='cursor:pointer'>";
how can I escape the quotes to get the following
<div class='col-sm-3' onclick='ViewItem("1")' style='cursor:pointer'>
We have all been there looking at code too long.
$item['item_id'] = '6';
echo "<div class='col-sm-3' onclick='ViewItem(\"".$item['item_id']."\")' style='cursor:pointer'>";
Output:
<div class='col-sm-3' onclick='ViewItem("6")' style='cursor:pointer'>

How can i scrape invalid html using php simple dom?

I'm trying to scrape a webpage using phpsimpledom.
$html = '<div class="namepageheader">
<div class="u">Name: Noor Shaad
<div class="u">Age: </div>
</div> '
$name=$html->find('div[class="u"]', 0)->innertext;
$age=$html->find('div[class="u"]', 1)->innertext;
I tried my best to get text from each class="u" but it didn't work because there is missing closing tag </div> on first tag <div class="u">. Can anyone help me out with that....
You can find an element close to where the tag should have been closed and then standardize the html by replacing it.
For example, you can replace the </a> tag by </a></div>.
str_replace('</a>','</a></div>',$html)
or if there are too many closed </a> tags , replace </a><div class="u"> with </a></div><div class="u">
str_replace('</a><div class="u">','</a></div><div class="u">',$html)
There may be another problem. There is a gap between the tags and the replacement does not work properly. To solve this problem, you can first delete the spaces between the tags and then replace them.
$html = '<div class="namepageheader">
<div class="u">Name: Noor Shaad
<div class="u">Age: </div>
</div> ' ;
$html = preg_replace('~>\\s+<~m', '><', $html);
str_replace('</a><div class="u">','</a></div><div class="u">',$html);
$name=$html->find('div[class="u"]', 0)->innertext;
$age=$html->find('div[class="u"]', 1)->innertext;

Replacing content between two div tags - PHP

I'm trying to replace content between two div tags using str_replace but I'm unsure how.
The content will be
<div class="profile-details">
<div class="username">Paradigm</div>
<div class="dob">01/01/2015</div>
</div>
What I want to do is replace the content between the <div class="profile-details">content </div> tags. The content is variable depending on the user profile.
Assuming this is a HTML string.
This would be my approach
echo preg_replace('/<div class="username">.+?</div>/im', '<div class="username">Special Username<\/div>', $string) ;

How to get content from Div which have other HTML tags using Regexp

I have div which contain other html tags along with text
I want to extract only text from this div OR inside all html tags
<div class="rpr-help m-chm">
<div class="header">
<h2 class="h6">Repair Help</h2>
</div><!-- /end .header -->
<div class="inner m-bsc">
<ul>
<li>Repair Video</li>
<li>Repair Q&A</li>
</ul>
</div>
<div>
<br>
<span class="h4">Cross Reference Information</span><br>
<p>Part Number 285753A (AP3963893) replaces 1195967, 280152, 285140, 285743, 285753, 3352470, 3363664, 3364002, 3364003, 62672, 62693, 661560, 80008, 8559748, AH1485646, EA1485646, PS1485646.
<br>
</p>
</div>
</div>
Here is my Regexp
preg_match_all("/<div class=\"rpr-help m-chm\">(.*)<\/.*>/s", $urlcontent, $description);
Its working fine whenever I assign this complete div to $urlcontent variable.
But when I am fetching data from real url like $urlcontent = "www.test.com/test.html";
its returning complete webpage script.
How can I get inside content of <div class="rpr-help m-chm"> ?
Is there any correction require in my regexp?
Any help would be appreciated. Thanks
It's not possible to parse HTML/XHTML by regex. Source
You can't parse [X]HTML with regex. Because HTML can't be parsed by
regex. Regex is not a tool that can be used to correctly parse HTML
Based on the language you use, Please consider using a thirdpart library for HTML parsing.
use this function
function GetclassContent($tagStart,$tagEnd,$content)
{
$first_step = explode( $tagStart,$content );
$second_step = explode($tagEnd,$first_step[1] );
return $second_step[0];
}
Steps to Use Above function
$website="www.test.com/test.html";
$content=file_get_contents($website);
$tagStart ='<div class="rpr-help m-chm">';
$tagEnd = "</div >";
$RequiredContent = GetclassContent($tagStart,$tagEnd,$content);

PHP echo inline CSS background url

I am trying to echo some inline CSS using PHP using this:
echo '<div class="image" style="background:url("img/testimage.jpg");width:300px;height:232px;">';
echo '</div>';
But for some reason this is returning this:
<div class="image" testimage.jpg");width:300px;height:232px;"="" img="" style="background:url("></div>
This is within a WordPress environment, am I doing something obvious wrong?
Escape the quotes inside the url declaration properly:
echo '<div class="image" style="background:url(\'img/testimage.jpg\'); width:300px; height:232px;">';
// ^ ^
you can't run something like this (in HTML) correctly:
style="background:url("img/testimage.jpg");width:300px;height:232px;"
must merge between single and duple quotes or escape them:
style="background:url('img/testimage.jpg');width:300px;height:232px;"
solution:
echo "<div class='image' style='background:url(\"img/testimage.jpg\");width:300px;height:232px;'></div>";
echo '<div class="image" style="background:url(\'img/testimage.jpg\');width:300px;height:232px;">';
echo '</div>';

Categories