I am using CURL to fetch form and store it in a field
..,
$str = curl_exec($ch);
The $str HTML has a textarea as follows
<td class="fntc">
Description
</td>
<td class="ffc">
<textarea name="descri" rows="6" class="emf" maxlength="128000">fictional.</textarea>
</td>
</tr>
Now I am trying to use a dom to fetch this area and was unsuccessful
$dom = new DOMDocument;
$dom->loadHTML($str);
// Get all the textarea field nodes
$inputs = $dom->getElementsByTagName('textarea');
// Iterate over the input fields and save the values we want to an array
foreach ($inputs as $input) {
$name = $input->getAttribute('name');
$val = $input->getAttribute('value');
$field_vals[$name] = $val;
}
But i am unable to get the value.Is there anything i am doing wrong here?
Since a <textarea> contains text inside the tag, rather than in a value attribute, you may access it with nodeValue:
$val = $input->nodeValue;
Update
Ok, I've verified this now:
$d = new DOMDocument();
$d->loadHTML("<html><head></head><body><textarea>textarea contents</textarea></body></html>");
$t = $d->getElementsByTagName("textarea");
foreach ($t as $tx) {
echo $tx->nodeValue;
}
// Prints
// textarea contents
Related
I want to extract the value of a specific cell from a table in a web page. First I search a string (here a player's name) and after I wan't to get the value of the <td> cell associated (here 94).
I can connect to the web page, find the table with is id and get all values. I also can search a specific string with preg_match but I can't extract the value of the <td> cell.
What the best way to extract the value of a table with a match expression ?
Here is my script :
<?php
// Connect to the web page
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
#$doc->loadHTMLFile('https://www.basketball-reference.com/leaders/trp_dbl_career.html');
$xpath = new DOMXPath($doc);
// Extract the table from is id
$table = $xpath->query("//*[#id='nba']")->item(0);
// See result in HTML
//$tableResult = $doc->saveHTML($table);
//print $tableResult;
// Get elements by tags and build a string
$str = "";
$rows = $table->getElementsByTagName("tr");
foreach ($rows as $row) {
$cells = $row -> getElementsByTagName('td');
foreach ($cells as $cell) {
$str .= $cell->nodeValue;
}
}
// Search a specific string (here a player's name)
$player = preg_match('/LeBron James(.*)/', $str, $matches);
// Get the value
$playerValue = intval(array_pop($matches));
print $playerValue;
?>
Here is the HTML structure of the table :
<table id="nba">
<thead><tr><th>Rank</th><th>Player</th><th>Trp Dbl</th></tr></thead>
...
<tr>
<td>5.</td>
<td><strong>LeBron James</strong></td>
<td>94</td>
</tr>
...
</table>
DOM manipulation solution.
Search over all cells and break if cell consists LeBron James value.
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
#$doc->loadHTMLFile('https://www.basketball-reference.com/leaders/trp_dbl_career.html');
$xpath = new DOMXPath($doc);
$table = $xpath->query("//*[#id='nba']")->item(0);
$str = "";
$rows = $table->getElementsByTagName("tr");
$trpDbl = null;
foreach ($rows as $row) {
$cells = $row->getElementsByTagName('td');
foreach ($cells as $cell) {
if (preg_match('/LeBron James/', $cell->nodeValue, $matches)) {
$trpDbl = $cell->nextSibling->nodeValue;
break;
}
}
}
print($trpDbl);
Regex expression for whole cell value with name LeBron James.
$player = preg_match('/<td>(.*LeBron James.*)<\/td>/', $str, $matches);
If you want to capture also ID 94 from next cell you can use this expression.
$player = preg_match('/<td>(.*LeBron James.*)<\/td>\s*<td>(.*)<\/td>/', $str, $matches);
It returns two groups, first cell with player's name and second with ID.
I need one help.I have multiple text field inside a div and i need to count those using PHP.I am explaining my code below.
<div class="questionshowp">
<input name="optional_0_0_ans" id="optional_0_0_ans" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text">
<input name="optional_0_1_ans" id="optional_0_1_ans" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text">
<input name="optional_0_2_ans" id="optional_0_2_ans" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text">
</div>
<?php
?>
Here 3 input fields are present inside a div.Here i need to echo how many nos of field is present using PHP.Please help me.
I am assuming that the HTML is not built/rendered using PHP, so then you can use the PHP built-in DOM parser.
<?php
//** Load the HTML **//
//$html = file_get_contents('http://www.url.com'); //Load the HTML from external webpage.
$html = file_get_contents(__FILE__); //Load the HTML from the current webpage.
//** Load HTML contents into DOM tree **//
$dom = new DOMDocument();
$dom->loadHTML($html);
//** Initialize DOM Parser **//
$finder = new DomXPath($dom);
//** Parse DOM for all occurences of <input> with parent <div> with class ="questionshowp' **//
$inputs = $finder->query("/html/body/div[#class='questionshowp']/input");
//** Determine how many input fields have type='text' **//
$count = 0;
foreach ($inputs as $input) {
if ($input->getAttribute('type') === 'text') {
$count++;
}
}
//** Print Results! **//
echo "Number of <input> fields where #type = 'text' is -> " .$count ."\n";
REFERENCE
http://php.net/manual/en/book.dom.php
Keep a flag $count and increment inside the for loop
$count = 0;
for loop {
$count++
}
echo $count;
or
$count = count($listValue);
I am trying to get the information from a html table into an php array. It is very easy to do with the following method:
function getdata($table)
{
$DOM = new DOMDocument;
$DOM->loadHTML($table);
$items = $DOM->getElementsByTagName('tr');
function tdrows($elements)
{
$str = "";
foreach ($elements as $element)
{
$str .= $element->nodeValue . ", ";
}
return $str;
}
foreach ($items as $node)
{
echo tdrows($node->childNodes) . "<br />";
}
}
The problem I am now facing is the content of the table has html inputs and I want just the value of those inputs. The table is of the form:
<table>
<tr><td><input type="text" /></td><td><input type="text" /></td><td><div class="add">add</div></td></tr>
</table>
Will I be able to modify the current function to accomplish this or should I try another approach
As PHP is server-side, you will not be able to get the value of the input unless you submit this inputted value to the server, for example using post or get when the form is submitted. You should use Javascript if you want the value of the input without having to submit the form. If this doesn't answer your question please try to ask it more clearly : )
I'm trying to parse an HTML document, and get text values from tags, but the problem is that the tags don't contain any special attributes or have some id's to target them.
The only thing that can be anchored to - is another static text, used as Labels.
The source page code looks similar to this
<tr>
<td>
<span>
Some text to link to
</span>
</td>
<td>
<span>
THE text to get
</span>
</td>
</tr>
/*****************Parser Page Script*************************/
$file = "src/src.htm";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
/********* Page that Processes *********/
//Pattern for regEx
$pattern = "/Some text to link to/";
$elements = $doc->getElementsByTagName('td');
if (!is_null($elements)) {
foreach ($elements as $node){
$text = $node->textContent;
if(preg_match($pattern, $text, $matches)){
echo "<pre>";
print_r($node);
echo "</pre>";
}
}
}
How to get the nextSibling value for searched td if the result is [nextSibling] => (object value omitted)?
A possibility is to use Xpath. Example xpath: /table/tr/td/span
$file = "src/src.htm";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$xpath = new DOMXpath($doc);
$elements = $xpath->query('/table/tr/td/span');
if(!empty($elements))
{
foreach($elements as $element)
{
echo $element->nodeValue;
}
}
I am trying to pull the href from a url from some data using php's domDocument.
The following pulls the anchor for the url, but I want the url
$events[$i]['race_1'] = trim($cols->item(1)->nodeValue);
Here is more of the code if it helps.
// initialize loop
$i = 0;
// new dom object
$dom = new DOMDocument();
//load the html
$html = #$dom->loadHTMLFile($url);
//discard white space
$dom->preserveWhiteSpace = true;
//the table by its tag name
$information = $dom->getElementsByTagName('table');
$rows = $information->item(4)->getElementsByTagName('tr');
foreach ($rows as $row)
{
$cols = $row->getElementsByTagName('td');
$events[$i]['title'] = trim($cols->item(0)->nodeValue);
$events[$i]['race_1'] = trim($cols->item(1)->nodeValue);
$events[$i]['race_2'] = trim($cols->item(2)->nodeValue);
$events[$i]['race_3'] = trim($cols->item(3)->nodeValue);
$date = explode('/', trim($cols->item(4)->nodeValue));
$events[$i]['month'] = $date['0'];
$events[$i]['day'] = $date['1'];
$citystate = explode(',', trim($cols->item(5)->nodeValue));
$events[$i]['city'] = $citystate['0'];
$events[$i]['state'] = $citystate['1'];
$i++;
}
print_r($events);
Here is the contents of the TD tag
<td width="12%" align="center" height="13"><!--mstheme--><font face="Arial"><span lang="en-us"><b>
<font style="font-size: 9pt;" face="Verdana">
<a linkindex="18" target="_blank" href="results2010/brmc5k10.htm">Overall</a>
Update, I see the issue. You need to get the list of a elements from the td.
$cols = $row->getElementsByTagName('td');
// $cols->item(1) is a td DOMElement, so have to find anchors in the td element
// then get the first (only) ancher's href attribute
// (chaining looks long, might want to refactor/check for nulls)
$events[$i]['race_1'] = trim($cols->item(1)->getElementsByTagName('a')->item(0)->getAttribute('href');
Pretty sure that you should be able to call getAttribute() on the item. You can verify that the item is nodeType XML_ELEMENT_NODE; it will return an empty string if the item isn't a DOMElement.
<?php
// ...
$events[$i]['race_1'] = trim($cols->item(1)->getAttribute('href'));
// ...
?>
See related: DOMNode to DOMElement in php