Skip to content

Commit 7b9464e

Browse files
committed
Made loadStr a public methor
Fixes paquettg#18
1 parent fc6cefb commit 7b9464e

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ $html = $dom->outerHtml;
9090

9191
As long as the Connector object implements the `PHPHtmlParser\CurlInterface` interface properly it will use that object to get the content of the url instead of the default `PHPHtmlParser\Curl` class.
9292

93+
Loading Strings
94+
---------------
95+
96+
Loading a string directly, with out the checks in `load()` is also easely done.
97+
98+
```php
99+
use PHPHtmlParser\Dom;
100+
101+
$dom = new Dom;
102+
$dom->loadStr('<html>String</html>', [])
103+
$html = $dom->outerHtml;
104+
```
105+
106+
If the string is to long, depending on your file system, the `load()` method will throw a warning. If this happens you can just call the above method to bypass the `is_file()` check in the `load()` method.
107+
93108
Options
94109
-------
95110

src/PHPHtmlParser/Dom.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,34 @@ public function loadFromUrl($url, $options = [], CurlInterface $curl = null)
160160
return $this->loadStr($content, $options);
161161
}
162162

163+
/**
164+
* Parsers the html of the given string. Used for load(), loadFromFile(),
165+
* and loadFromUrl().
166+
*
167+
* @param string $str
168+
* @param array $option
169+
* @chainable
170+
*/
171+
public function loadStr($str, $option)
172+
{
173+
$this->options = new Options;
174+
$this->options->setOptions($this->globalOptions)
175+
->setOptions($option);
176+
177+
$this->rawSize = strlen($str);
178+
$this->raw = $str;
179+
180+
$html = $this->clean($str);
181+
182+
$this->size = strlen($str);
183+
$this->content = new Content($html);
184+
185+
$this->parse();
186+
$this->detectCharset();
187+
188+
return $this;
189+
}
190+
163191
/**
164192
* Sets a global options array to be used by all load calls.
165193
*
@@ -291,34 +319,6 @@ public function getElementsByClass($class)
291319
return $this->find('.'.$class);
292320
}
293321

294-
/**
295-
* Parsers the html of the given string. Used for load(), loadFromFile(),
296-
* and loadFromUrl().
297-
*
298-
* @param string $str
299-
* @param array $option
300-
* @chainable
301-
*/
302-
protected function loadStr($str, $option)
303-
{
304-
$this->options = new Options;
305-
$this->options->setOptions($this->globalOptions)
306-
->setOptions($option);
307-
308-
$this->rawSize = strlen($str);
309-
$this->raw = $str;
310-
311-
$html = $this->clean($str);
312-
313-
$this->size = strlen($str);
314-
$this->content = new Content($html);
315-
316-
$this->parse();
317-
$this->detectCharset();
318-
319-
return $this;
320-
}
321-
322322
/**
323323
* Checks if the load methods have been called.
324324
*

0 commit comments

Comments
 (0)