Call to undefined method DOMDocument::getElementsByClassName()
Class DOMDocument does not contains method getElementsByClassName
Use xpath
$xpath = new DOMXpath($dom);
$xpath->query('//div[contains(@class, "localImage")]'); //instance of DOMNodeList
Get Element by ClassName with DOMdocument() Method
I finally used the following solution :
$classname="blockProduct";
$finder = new DomXPath($doc);
$spaner = $finder->query("//*[contains(@class, '$classname')]");
Get text of element using DOM with PHP but return error
There isn't a getElementsByClassName
function. You can either iterate over all elements of a specific type with https://www.php.net/manual/en/domdocument.getelementsbytagname.php then check their class for specifed value, https://www.php.net/manual/en/domelement.getattribute.php. Alternatively you can use an xpath
.
$html = '<table>
<tbody>
<tr><tr>
<tr>
<td>
<table class="style124">
<tbody>
<tr>
<td>A</td>
</tr>
<tr>
<td>B</td> //THIS !!
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
</tbody>
</table>';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$query = '//table[contains(@class, "style124")]/tbody/tr[2]/td';
$entries = $xpath->query($query);
foreach($entries as $entry){
echo $entry->nodeValue;
}
https://3v4l.org/pDFhB
Additionally, getElementsByTagName
returns a result set, you wouldn't just access it with ->
you'd need to tell it which you wanted.
The alternative approach could be:
$html = '<table>
<tbody>
<tr><tr>
<tr>
<td>
<table class="style124">
<tbody>
<tr>
<td>A</td>
</tr>
<tr>
<td>B</td> //THIS !!
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
</tbody>
</table>';
$dom = new DOMDocument;
$dom->loadHTML($html);
$tables = $dom->getElementsByTagName('table');
foreach($tables as $table){
if(preg_match('/\bstyle124\b/', $table->getAttribute('class'))){
$trs = $table->getElementsByTagName('tr');
$tds = $trs[1]->getElementsByTagName('td');
echo $tds[0]->nodeValue;
}
}
note the base index difference between PHP and xpath, $trs[1]
vs. tr[2]
.
https://3v4l.org/hLEFV
Can't find error with xpath query, `undefined method DOMDocument`
There isn't such method like xpath
in DOMDocument.
See http://php.net/manual/en/class.domdocument.php
You need to initialize an instance of DOMXpath to use xpath queries
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
or alternatively call the method on the SimpleXMLObject you've created
$level1 = $xml->xpath('//*[contains(concat(" ", normalize-space(@class), " "), " category-wrap ")]/h2/a');
Call to undefined method DOMDocument::createDocumentType()
DOMDocument
does not have a method named createDocumentType
, as you can see in the Manual. The method belongs to the DOMImplemetation class. It is used like this (taken from the manual):
// Creates an instance of the DOMImplementation class
$imp = new DOMImplementation;
// Creates a DOMDocumentType instance
$dtd = $imp->createDocumentType('graph', '', 'graph.dtd');
// Creates a DOMDocument instance
$dom = $imp->createDocument("", "", $dtd);
Since you want to load HTML into the document, you don't need to specify a document type, since it is determined from the imported HTML. You just have to have some id
attributes, or a DTD that identifies an other attribute as an id. This is part of the HTML file, not the parsing PHP code.
$dom = new DOMDocument();
$dom->loadHTML($result);
$element = $dom->getElementById('my_id');
will do the job.
Fatal error: Call to undefined method DOMDocument::getElementsById()
getElementsById()
is not a method of DOMDocument
, you should try getElementById()
instead. I don't even think two elements can have the same id, so you won't be able to get a collection (array) based on id.
PHP Xpath: Get node value by class name
har07's answer was on the right track, but it only returned the node list with length set to 3 like I was already receiving with my existing code.
Original code:
$productPrice = $productRaw[1]->getElementsByTagName('li');
har07's suggestion:
$productPrice = $xpath->query('.//li[contains(@class, "itemtwo")]', $productRaw->item(1));
Solution, which returns the node value where an elements class name is equal to itemtwo
:
$productPrice = $xpath->query('.//li[contains(@class, \'itemtwo\')]', $productRaw[1])->item(1)->nodeValue;
php to extract data from a website
You need first div
's p
elements only, so your query would be:
$entries2 = $xpath->query('//(div[@class='entry-content'])[1]/p');
Now you can iterate all p
elements with foreach()
loop (extracting its html contents):
$innerHtml = '';
foreach ($entries2 as $entry) {
$children = $entry->childNodes;
foreach ($children as $child) {
$innerHtml .= $child->ownerDocument->saveXML($child);
}
}
$innerHtml = str_replace(["\r\n", "\r", "\n", "\t"], '', $innerHtml);
Related Topics
Yii2 Translation Does Not Work
Stored Procedures, MySQL and PHP
Difference Between Mt_Rand() and Rand()
Increase PHP Script Execution Time
Php: Do Arrays Have a Maximum Size
Laravel 5.1: Keep Uploaded File as Old Input
How to Use Array_Unique on an Array of Arrays
Domxpath - Get Href Attribute and Text Value of an a Element
File_Exists() Returns False Even If File Exist (Remote Url)
Mysqli_Select_Db() Expects Parameter 1 to Be MySQLi, String Given
How to Override Built-In PHP Function(S)
How Come Checkbox State Is Not Always Passed Along to PHP Script
Retrieve (Or Simulate) Full Query from Pdo Prepared Statement
PHP MySQL Greek Letters Showing Like? Marks
In PHP, How to Check If a Function Exists
How to Get the Last N Items in a PHP Array as Another Array
Increasing PHP Memory_Limit. at What Point Does It Become Insane