Get textbox value using PHP

Perhaps someone knows how to get the value of a specific element in an HTML document with PHP? What I'm doing right now is using file_get_contentsto pull the HTML code from another site, and this website has a text box:

<textarea id="body" name="body" rows="12" cols="75" tabindex="1">Hello World!</textarea>

What I want to do is make a script file_get_contentsand just pull out "Hello World!". from the text box. Is it possible? Sorry for listening to you guys again, you are giving such useful advice:].

+3
source share
2 answers

, , . PHP Simple HTML DOM Parser, , :

$html     = file_get_html('http://www.domain.com/');
$textarea = $html->find('textarea[id=body]'); 
$contents = $textarea->innertext;

echo $contents; // Outputs 'Hello World!'

file_get_contents(), :

$raw_html = file_get_contents('http://www.domain.com/');
$html     = str_get_html($raw_html);
...

file_get_contents(), outertext, , HTML-, -:

$html     = file_get_html('http://www.domain.com/');
$raw_html = $html->outertext;

:

preg_match('~<textarea id="body".*?>(.*?)</textarea>~', file_get_contents('http://www.domain.com/'), $matches);
echo $matches[1][0]; // Outputs 'Hello World!'

, , .

+7

PHP DOM DOMXPath.

$dom = DOMDocument::loadHTMLFile( $url );
$xpath = new DOMXPath( $dom );
$nodes = $xpath->query('//textarea[id=body]' )

$result = array();
for( $nodes as $node ) {
    $result[] = $node->textContent;
}

$result id.

+2

Source: https://habr.com/ru/post/1733795/


All Articles