i just started using curl, please tell me how can i save the td value or innerhtml into a variable. here is my code:
$xx = "mylink.html";
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $xx);
curl_setopt($cURL, CURLOPT_HEADER,0);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL, CURLOPT_COOKIESESSION, true);
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, 1);
$strPage = curl_exec($cURL);
curl_close($cURL);
$pattern = '#<tbody>(.*?)</tbody>#';
if( preg_match($pattern, $strPage, $match)){
$catch = $match[0];
print_r($match[0]);
}else{
echo 'Could not find it';
}
here is the output:
<tbody class="highlighted"><tr class="xx"><td>value1</td><td>value2</td><td>value3</td><td>value4</td><td>value5</td></tr></tbody>
My Answer:
Sure! You can use the `DOMDocument` class in PHP to parse the HTML content and extract the values of the `
php
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, 'http://example.com'); // URL of the webpage
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL session
$response = curl_exec($ch);
// Close cURL session
curl_close($ch);
// Create a new DOMDocument object
$dom = new DOMDocument();
$dom->loadHTML($response);
// Get all elements
$tds = $dom->getElementsByTagName('td');
// Loop through each element and save its value to a variable
foreach ($tds as $td) {
$tdValue = $td->nodeValue;
// Do something with $tdValue
echo $tdValue . "\n";
}
In this code snippet, we first make a cURL request to fetch the HTML content of a webpage. Then, we use the `DOMDocument` class to parse the HTML content and extract the values of the ` ` elements. Finally, we loop through each ` ` element and save its value to a variable `$tdValue`. You can modify the code to store the values in an array or any other data structure as per your requirement.
© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog
|
Privacy Policy
|
Terms & Conditions
|
Contact Us
Rate this post
4 of 5 based on 3658 votesComments