php use preg_replace remove div - php

I tried use preg_replace remove the div which class="image content", I use some code below, but still remain two </div> after my preg_replace, need a help, thanks.
<style>
#contract{width:100%;height:100%;}
#content{width:1002px;overflow:hidden;margin:0 auto;}
</style>
<div id="contract">
<div id="content">
<?php
$html = <<<EOT
<div style="float:left;" class="image content"><div style="float:left;"><div style="float:left;">
<img alt="Se hai problemi nella visualizzazione dei caratteri, clicca qui." src="../../image/1001.jpg" >
</div></div></div>
<div class="content" style="float:left;"><i>some content here.</i></div>
EOT;
echo preg_replace('/<div(.*?)class="image content">([\s\S]*?)<\/div>/','',$html);
?>
</div>
</div>
I need return: <div class="content" style="float:left;"><i>some content here.</i></div>

I know it's not answer to this question, but don't use regular expressions for manipulating with DOM. There are specialized classes for that (DOMDocument), which are faster and will cause you less headache.

Related

How access nested elements with repeated class xpath

I need access a nested element with repeated class, like that:
<div class="container">
<div class="first"></div>
<div class="first"></div>
<div class="first">
<div class="second"></div>
<div class="second">
<p>I need that text</p>
</div>
</div>
</div>
So i try something like that:
$localizacao_x = $xpath_det_page->query('//div[#class="container"]/div[#class="first"][3]/div[#class="second"][2]/p');
$localizacao = $localizacao_x->item(0)->nodeValue;
echo "[Localizacao] : [".$localizacao."]"."<br/>";
But result in non object, any tip?
Your XPath seems to be correct. I tested
//div[#class="container"]/div[#class="first"][3]/div[#class="second"][2]/p
which result is
I need that text

Couldnt grab div element with a specified class name using simple_html_dom?

I am using simple_html_dom, i am having issues grabbing a div with a class name specified below is the code!
<?php
include 'simple_html_dom.php';
$html='
<div class="user-info ">
<div class="user-action-time">
answered <span title="2016-06-27 20:01:45Z" class="relativetime">Jun 27 at 20:01</span>
</div>
<div class="user-gravatar32">
<div class="gravatar-wrapper-32"><img src="https://www.gravatar.com/avatar/09e3746cf7e47d4b3b15f5d871b91661?s=32&d=identicon&r=PG" alt="" width="32" height="32"></div>
</div>
<div class="user-details">
David Mulder
<div class="-flair">
';
echo $html->find('div[class=user-details]',0);
?>
What am i doing wrong here i am getting error Call to a member function find() on string in
Thanks!
You are tying to use Simple Html Dom to parse an html string.
Do not assign your html string to $html variable.
Assign it to an other, like $html_string.
Then use $html = str_get_html($html_string)
and
echo $html->find('div[class=user-details]',0);
You trying to call object method on a string variable. It should works:
$html = str_get_html('<div class="user-info ">
<div class="user-action-time">
answered <span title="2016-06-27 20:01:45Z" class="relativetime">Jun 27 at 20:01</span>
</div>
<div class="user-gravatar32">
<div class="gravatar-wrapper-32"><img src="https://www.gravatar.com/avatar/09e3746cf7e47d4b3b15f5d871b91661?s=32&d=identicon&r=PG" alt="" width="32" height="32"></div>
</div>
<div class="user-details">
David Mulder
<div class="-flair">');

Xpath Exclude p.class of a div

This is my HTML example:
<div id="Texte">
<div class="pagination">
...
</div>
<p>...</p>
<p>....</p>
<p class="Foot">...</p>
</div>
I want to use Xpath to get all content of my <div id="Texte"> without the <p class="foot">.
I use this, but it's not ok, I have the class='Foot' in my result :
$crawler->filterXPath("//*[#id='Texte' and not(#class='Foot')]")->html();
Almost.
// correct
$crawler->filterXPath("//*[#id='Texte']/*[not(#class='Foot')]")->html();
// yours, for comparison
$crawler->filterXPath("//*[#id='Texte' and not(#class='Foot')]")->html();

select children of the first element of a certain class using XPath

i have this type of code:
<div class="content">
<p></p>
<p></p>
<p></p>
</div>
<div class="content">
<p></p>
<p></p>
<p></p>
</div>
i wish to select all p elements from the first element with the class content.
i managed to select the first class by using:
(//div[#class="content"])[1]
but using (//div[#class="content"])[1]/p it still shows both classes
Here's an working example using PHP's SimpleXML. I've made some small changes to the HTML code you provided so the output would be more meaningful.
Regarding the XPath expression you provided I just removed the parenthesis and it all worked as expected.
NOTE: Following #LarsH's comment, I reverted the XPath expression as it was OK for starters. I took the liberty to update it based on its example.
<?php
$html = <<<HTML
<body>
<div class="content">
<p>1</p>
<p>2</p>
<p>3</p>
</div>
<div class="content">
<p>4</p>
<p>5</p>
<p>6</p>
</div>
<div>
<div class="content">
<p>7</p>
<p>8</p>
<p>9</p>
</div>
</div>
</body>
HTML;
$sxe = new SimpleXMLElement($html);
foreach ($sxe->xpath('(//div[#class="content"])[1]/p') as $p) {
echo "$p\n";
}
Output:
1
2
3
Link to codepad working example.

PhpQuery and replaceWith, How to?

I'm using PhpQuery and I need to replace an "iframe" for another tag
The html file have an Iframe
<div id="content">
<div class="pad3"></div>
<iframe src="http://www.yahoo.com" id="iFrame"></iframe>
<div class="pad2"></div>
</div>
Whit this piece of
$doc = phpQuery::newDocumentFileHTML('file.htm');
$doc->find('iframe')->replaceWith('<p>test</p>');
I expected this:
<div id="content">
<div class="pad3"></div>
<p>test</p>
<div class="pad2"></div>
</div>
But nothing happens. Can someone give me some clues?
Best Regards
Try using the id of your iframe element:
$doc->find('#iFrame')->replaceWith('<p>test</p>');

Categories