I have little problem with parsing some data from one webpage.
I'm trying to get class name of certain div.
Example:
< div class="stars b3"></div>
I want to save in array just b3.
Is it possible to do this?
Thanks!
See this:
<?php // http://stackoverflow.com/questions/4835300/php-dom-to-get-tag-class-with-multiple-css-class-name
$html = <<< HTML
<td class="pos" >
<a class="firstLink" href="Search/?List=200003000112097&sr=1" >
Firs link value
</a>
<br />
<a class="secondLink SecondClass" href="/Search/?KeyOpt=ALL" >
Second Link Value
</a>
</td
HTML;
$dom = new DOMDocument();
#$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate(
"/html/body//a[#class='secondLink SecondClass']"
);
echo $hrefs->item(0)->getAttribute('class');
Ref. http://codepad.org/VZVUXgrT
Related
I create own WYSIWYG and I want to get his content (its div)
I try to do this:
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$divContent = $xpath->query('//div[id="editor"]');
$_POST['details'] = $divContent;
<div id="editor" style='height:500px;' name="details" contenteditable required>
<p></p>
</div>
I want to get the div that have the id editor content, but its return nothing.
Now you find div with item id. To select attribute use #
$divContent = $xpath->query('//div[#id="editor"]');
To get outterHTML of div
echo $dom->saveHTML($divContent->item(0));
demo
hello I have a php regex code like this :
preg_replace('~<div\s*.*?(?:\s*class\s*=\s*"(.*?)"|id\s*=\s*"(.*?)\s*)?>~i','<div align="center" class="$1" id="$2">', "html source code");
now what I want to do is to replace all tags in the source html code and then keep only the class and id from the div tag plus add align="center" to it:
examples:
<div style="border:none;" class="classbutton"> will be replaced to <div align="center" class="classbutton">
<div style="border:none;" class="classbutton" id="idstyle"> will be replaced to <div align="center" class="classbutton" id="idstyle">
I already tried many codes using php regex but nothing seems to be working for me. so if someone can help me or give me a domdocument code to fix this issue.
thanks in advance.
Here is some snippet that should get you going:
$html = '<body><div style="border:none;" class="classbutton" id="idstyle">Some text</div></body>'; // Sample HTML string
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
$divs = $xpath->query('//div[#class="classbutton"]'); // Get all DIV tags with class "classbutton"
foreach($divs as $div) { // Loop through all DIVs found
$div->setAttribute('align', 'center'); // Set align="center"
$div->removeAttribute('style'); // Remove "style" attribute
}
echo $dom->saveHTML(); // Save HTML (use $html = $dom->saveHTML();)
See IDEONE demo
I have this html:
<a href=" URL TO KEEP" class="class_to_check">
<strong> TEXT TO KEEP</strong>
</a>
I have a long html code with many link as above, I have to keep the links that have the <strong> inside, I have to keep the HREF of the link and the text inside the <strong>, how can i do using DOMDocument?
Thank you!
$html = "...";
$dom = new DOMDOcument();
$dom->loadHTML($html);
$xp = new XPath($dom);
$a = $xp->query('//a')->item(0);
$href = $a->getAttribute('href');
$strong = $a->nodeValue;
Of course, this XPath stuff works for just this particular html snippet. You'll have to adjust it to work with a more fully populated HTML tree.
I have difficulties to get second link href and Text. How to select class="secondLink SecondClass". Using PHP Dom, Thank you
<td class="pos" >
<a class="firstLink" href="Search/?List=200003000112097&sr=1" >
Firs link value
</a>
<br />
<a class="secondLink SecondClass" href="/Search/?KeyOpt=ALL" >
Second Link Value
</a>
</td
My code is
// parse the html into a DOMDocument
$dom = new DOMDocument();
#$dom->loadHTML($html);
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
// grab all the on the page
$xpath = new DOMXPath($dom);
//$hrefs = $xpath->evaluate("/html/body//a[#class='firstLink']");// its working
$hrefs = $xpath->evaluate("/html/body//a[#class='secondLink SecondClass']");// not working
Thank you
$hrefs = $xpath->evaluate("/html/body//a[contains(concat(' ',#class,' '),' secondClass ')
and (contains(concat(' ',#class,' '),' secondLink '))]"
from this answer
you can pick it by selecting your td having class pos and selecting anchor tags. then you cann control your returing array to get your specific anchor tag
I would like to place a new node element, before a given element. I'm using insertBefore for that, without success!
Here's the code,
<DIV id="maindiv">
<!-- I would like to place the new element here -->
<DIV id="child1">
<IMG />
<SPAN />
</DIV>
<DIV id="child2">
<IMG />
<SPAN />
</DIV>
//$div is a new div node element,
//The code I'm trying, is the following:
$maindiv->item(0)->parentNode->insertBefore( $div, $maindiv->item(0) );
//Obs: This code asctually places the new node, before maindiv
//$maindiv object(DOMNodeList)[5], from getElementsByTagName( 'div' )
//echo $maindiv->item(0)->nodeName gives 'div'
//echo $maindiv->item(0)->nodeValue gives the correct data on that div 'some random text'
//this code actuall places the new $div element, before <DIV id="maindiv>
http://pastie.org/1070788
Any kind of help is appreciated, thanks!
If maindiv is from getElementsByTagName(), then $maindiv->item(0) is the div with id=maindiv. So your code is working correctly because you're asking it to place the new div before maindiv.
To make it work like you want, you need to get the children of maindiv:
$dom = new DOMDocument();
$dom->load($yoursrc);
$maindiv = $dom->getElementById('maindiv');
$items = $maindiv->getElementsByTagName('DIV');
$items->item(0)->parentNode->insertBefore($div, $items->item(0));
Note that if you don't have a DTD, PHP doesn't return anything with getElementsById. For getElementsById to work, you need to have a DTD or specify which attributes are IDs:
foreach ($dom->getElementsByTagName('DIV') as $node) {
$node->setIdAttribute('id', true);
}
From scratch, this seems to work too:
$str = '<DIV id="maindiv">Here is text<DIV id="child1"><IMG /><SPAN /></DIV><DIV id="child2"><IMG /><SPAN /></DIV></DIV>';
$doc = new DOMDocument();
$doc->loadHTML($str);
$divs = $doc->getElementsByTagName("div");
$divs->item(0)->appendChild($doc->createElement("div", "here is some content"));
print_r($divs->item(0)->nodeValue);
Found a solution:
$child = $maindiv->item(0);
$child->insertBefore( $div, $child->firstChild );
I don't know how much sense this makes, but well, it worked.