function Crawler::addContent
Adds HTML/XML content.
If the charset is not set via the content type, it is assumed to be UTF-8, or ISO-8859-1 as a fallback, which is the default charset defined by the HTTP 1.1 specification.
1 call to Crawler::addContent()
- Crawler::add in vendor/
symfony/ dom-crawler/ Crawler.php - Adds a node to the current list of nodes.
File
-
vendor/
symfony/ dom-crawler/ Crawler.php, line 132
Class
- Crawler
- Crawler eases navigation of a list of \DOMNode objects.
Namespace
Symfony\Component\DomCrawlerCode
public function addContent(string $content, ?string $type = null) : void {
if (!$type) {
$type = str_starts_with($content, '<?xml') ? 'application/xml' : 'text/html';
}
// DOM only for HTML/XML content
if (!preg_match('/(x|ht)ml/i', $type, $xmlMatches)) {
return;
}
$charset = preg_match('//u', $content) ? 'UTF-8' : 'ISO-8859-1';
// http://www.w3.org/TR/encoding/#encodings
// http://www.w3.org/TR/REC-xml/#NT-EncName
$content = preg_replace_callback('/(charset *= *["\']?)([a-zA-Z\\-0-9_:.]+)/i', function ($m) use (&$charset) {
if ('charset=' === $this->convertToHtmlEntities('charset=', $m[2])) {
$charset = $m[2];
}
return $m[1] . $charset;
}, $content, 1);
if ('x' === $xmlMatches[1]) {
$this->addXmlContent($content, $charset);
}
else {
$this->addHtmlContent($content, $charset);
}
}