Şu sınıf işinizi kolaylaştırabilir.
Selectordom.php
<?php
class Selectordom {
public function get($url) {
$dom = new DOMDocument();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSLVERSION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$buffer = curl_exec($ch);
curl_close($ch);
//$buffer = mb_convert_encoding($buffer, 'HTML-ENTITIES', 'UTF-8');
@$dom->loadHTML($buffer);
$this->xpath = new DOMXpath($dom);
}
public function select($selector, $as_array = true) {
$elements = $this->xpath->evaluate($this->selector_to_xpath($selector));
return $as_array ? $this->elements_to_array($elements) : $elements;
}
public function elements_to_array($elements) {
$array = array();
for ($i = 0, $length = $elements->length; $i < $length; ++$i)
if ($elements->item($i)->nodeType == XML_ELEMENT_NODE)
array_push($array, $this->element_to_array($elements->item($i)));
return $array;
}
public function innerXML($node) {
$doc = $node->ownerDocument;
$frag = @$doc->createDocumentFragment();
foreach ($node->childNodes as $child) {
@$frag->appendChild($child->cloneNode(TRUE));
}
return (string) $doc->saveXML($frag);
}
public function element_to_array($element) {
$array = array(
'name' => $element->nodeName,
'attributes' => array(),
'text' => $element->textContent,
'html' => $this->innerXML($element),
'children' => $this->elements_to_array($element->childNodes)
);
if ($element->attributes->length)
foreach($element->attributes as $key => $attr)
$array['attributes'][$key] = $attr->value;
return $array;
}
public function selector_to_xpath($selector) {
$selector = preg_replace('/\s*>\s*/', '>', $selector);
$selector = preg_replace('/\s*~\s*/', '~', $selector);
$selector = preg_replace('/\s*\+\s*/', '+', $selector);
$selector = preg_replace('/\s*,\s*/', ',', $selector);
$selectors = preg_split('/\s+(?![^\[]+\])/', $selector);
foreach ($selectors as &$selector) {
// ,
$selector = preg_replace('/,/', '|descendant-or-self::', $selector);
// input:checked, :disabled, etc.
$selector = preg_replace('/(.+)?:(checked|disabled|required|autofocus)/', '\1[@\2="\2"]', $selector);
// input:autocomplete, :autocomplete
$selector = preg_replace('/(.+)?:(autocomplete)/', '\1[@\2="on"]', $selector);
// input:button, input:submit, etc.
$selector = preg_replace('/:(text|password|checkbox|radio|button|submit|reset|file|hidden|image|datetime|datetime-local|date|month|time|week|number|range|email|url|search|tel|color)/', 'input[@type="\1"]', $selector);
// foo[id]
$selector = preg_replace('/(\w+)\[([_\w-]+[_\w\d-]*)\]/', '\1[@\2]', $selector);
// [id]
$selector = preg_replace('/\[([_\w-]+[_\w\d-]*)\]/', '*[@\1]', $selector);
// foo[id=foo]
$selector = preg_replace('/\[([_\w-]+[_\w\d-]*)=[\'"]?(.*?)[\'"]?\]/', '[@\1="\2"]', $selector);
// [id=foo]
$selector = preg_replace('/^\[/', '*[', $selector);
// div#foo
$selector = preg_replace('/([_\w-]+[_\w\d-]*)\#([_\w-]+[_\w\d-]*)/', '\1[@id="\2"]', $selector);
// #foo
$selector = preg_replace('/\#([_\w-]+[_\w\d-]*)/', '*[@id="\1"]', $selector);
// div.foo
$selector = preg_replace('/([_\w-]+[_\w\d-]*)\.([_\w-]+[_\w\d-]*)/', '\1[contains(concat(" ",@class," ")," \2 ")]', $selector);
// .foo
$selector = preg_replace('/\.([_\w-]+[_\w\d-]*)/', '*[contains(concat(" ",@class," ")," \1 ")]', $selector);
// div:first-child
$selector = preg_replace('/([_\w-]+[_\w\d-]*):first-child/', '*/\1[position()=1]', $selector);
// div:last-child
$selector = preg_replace('/([_\w-]+[_\w\d-]*):last-child/', '*/\1[position()=last()]', $selector);
// :first-child
$selector = str_replace(':first-child', '*/*[position()=1]', $selector);
// :last-child
$selector = str_replace(':last-child', '*/*[position()=last()]', $selector);
// :nth-last-child
$selector = preg_replace('/:nth-last-child\((\d+)\)/', '[position()=(last() - (\1 - 1))]', $selector);
// div:nth-child
$selector = preg_replace('/([_\w-]+[_\w\d-]*):nth-child\((\d+)\)/', '*/*[position()=\2 and self::\1]', $selector);
// :nth-child
$selector = preg_replace('/:nth-child\((\d+)\)/', '*/*[position()=\1]', $selector);
// :contains(Foo)
$selector = preg_replace('/([_\w-]+[_\w\d-]*):contains\((.*?)\)/', '\1[contains(string(.),"\2")]', $selector);
// >
$selector = preg_replace('/>/', '/', $selector);
// ~
$selector = preg_replace('/~/', '/following-sibling::', $selector);
// +
$selector = preg_replace('/\+([_\w-]+[_\w\d-]*)/', '/following-sibling::\1[position()=1]', $selector);
$selector = str_replace(']*', ']', $selector);
$selector = str_replace(']/*', ']', $selector);
}
// ' '
$selector = implode('/descendant::', $selectors);
$selector = 'descendant-or-self::' . $selector;
// :scope
$selector = preg_replace('/(((\|)?descendant-or-self::):scope)/', '.\3', $selector);
// $element
$sub_selectors = explode(',', $selector);
foreach ($sub_selectors as $key => $sub_selector) {
$parts = explode('$', $sub_selector);
$sub_selector = array_shift($parts);
if (count($parts) && preg_match_all('/((?:[^\/]*\/?\/?)|$)/', $parts[0], $matches)) {
$results = $matches[0];
$results[] = str_repeat('/..', count($results) - 2);
$sub_selector .= implode('', $results);
}
$sub_selectors[$key] = $sub_selector;
}
$selector = implode(',', $sub_selectors);
return $selector;
}
}test.php
<?php
header('Content-type: application/json');
include 'Selectordom.php';
$Selector = new Selectordom();
$Selector->get('https://www.r10.net/php/2985702-preg_match_all-yardim.html');
exit(json_encode($Selector->select('.userSignature')));