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
Related
So, I have a JSON array, which parses just fine in the layout view, but the elements from json stack vertically and I need them to be horizontally aligned.
Here is the example code
<div class="swiper-container mySwiper2 swiper-container-h">
<div class="swiper-wrapper">
<div class="swiper-slide swiper-padding">
<?php include_once("team_hq.php"); ?>
</div>
<div class="swiper-slide swiper-padding bkg bkg3">
<h2>Partners</h2>
<?php
showStuff($stuff, "Partners"); ?>
</div>
<div class="swiper-slide swiper-padding bkg bkg12">
<h2>Production</h2>
<?php showStuff($stuff, "Production"); ?>
</div>
<div class="swiper-slide swiper-padding bkg bkg13">
<h2>Sales</h2>
<?php showStuff($stuff, "Sales"); ?>
</div>
<div class="swiper-slide swiper-padding bkg bkg14">
<h2>Support</h2>
<?php showStuff($stuff, "Support"); ?>
</div>
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-pagination"></div>
</div>
The question I'm asking is how can I make the content which is pulled from the $stuff variable present horizontally instead of vertically as is now.
To position HTML elements horizontal either use:
Flexbox or CSS-Grid or Floating
But assuming your function showStuff just prints a JSON string, you'd have to work with your $stuff variable. E.g. loop over the it's content – with PHP I suppose – and build the HTML-Tags according your needs.
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();
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.
With jQuery or PHP, I would like to modify the dom structure of the the_content function in WordPress. In some posts I use the h3 element, and I would like to add a wrapper that contains the content until the next h3.
So I would like to convert this:
<h3>Title</h3>
<p>This is just regular text</p>
<h3>Next title</h3>
Into this:
<div class='wrapper'>
<h3>Title</h3>
<p>This is just regular text</p>
</div>
<div class='wrapper'>
<h3>Next title</h3>
</div>
Thanks!
assuming that the content only consists of <h3>s and <p>s,and they are welled formated like:
<div id="content">
<h3>title</h3>
<p>..........</p>
<p>..........</p>
<h3>another title</h3>
<p>.........</p>
<h3>yet another title</h3>
<p>..........</p>
</div>
then you may try this in jQuery.
//get the main post content
$content=$("#content");
$h3s=$content.find('h3');
$h3s.each(function(index){
if(index==0)$(this).before('<div class="wrapper">');
else $(this).before('</div><div class="wrapper">');
});
//remeber to close the last one
$content.append('</div>');
There have some texts, how to use php regular get the 2ed and 3rd <div class="partright">? Thanks.
<div class="wrap">
<div class="content>
<div class="partleft">
text1
</div>
<div class="partright">
text2
</div>
</div>
<div class="content>
<div class="partleft">
text3
</div>
<div class="partright">
text4
</div>
</div>
<div class="content>
<div class="partleft">
text5
</div>
<div class="partright">
text6
</div>
</div>
</div>
I want output
<div class="partright">
text4
</div>
<div class="partright">
text6
</div>
Your question is very incomplete but I assume your talking about traversing the elements to modify them in some way.
You should look at the following library called SimpleDOM
And usage would be like:
require_once 'simple_dom.class.php';
$html = "<html_data_here>";
$html = str_get_html($html);
foreach($html->find(".partleft:nth(2),.partleft:nth(3)") as $p)
{
echo $p->outerText;
}
Note: The above is an example and may not work as expected, for working examples please see the Simple Dom site linked above.
You can't parse [X]HTML with regex. RegEx match open tags except XHTML self-contained tags use SimpleXML indeed.
You could build a regular expression that would match
<div class="partright">(.*)</div>
Put all matches in an array and take the 2nd and third element from the array.