Parse HTML Table - PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an HTML table that I would like to parse in PHP to store into a MySQL Database. The HTML looks like this:
<tr><td>DATE</td><td>LOCATION</td><td>NAME</td></tr>
I would like to create a PHP function that returns in an array, the fields in capital letters. Does anyone know any php libraries that can do this, or should I be using a different language, as this may be complex. I don't know exactly how to do this with many tables on the page, but I am trying to parse the VEX events on RobotEvents. The table that I want to parse starts at line 465.

Take a look at the PHP HTML DOM Parser library.
To use, you can do something similar to this (not my example):
require('simple_html_dom.php');
$table = array();
$html = file_get_html('http://flow935.com/playlist/flowhis.HTM');
foreach($html->find('tr') as $row) {
$time = $row->find('td',0)->plaintext;
$artist = $row->find('td',1)->plaintext;
$title = $row->find('td',2)->plaintext;
$table[$artist][$title] = true;
}
echo '<pre>';
print_r($table);
echo '</pre>';
There's some tutorials, SO questions and interesting reads about the library. It seems to be pretty popular.
http://davidwalsh.name/php-notifications
http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/
Looping through a table with Simple HTML DOM
how to print cells of a table with simple html dom
UPDATE FOR FINDING SPECIFIC TABLE IN HTML USING ABOVE LIBRARY
To find a particular table amongst many:
1. By class:
On line 465 of your scraped HTML, the table starts with a class catalog-listing, so:
foreach ($html->find('table[#class="catalog-listing"]')->find('tr') as $row) {
// extract TD data
}
2. By instance (find 2nd table in HTML)
foreach ($html->find('table', 2)->find('tr') as $row) {
// extract TD data
}

As you're prepared to look beyond PHP, Nokogiri (Ruby) and Beautiful Soup (Python) are well-established libraries that parse HTML very well.
That doesn't imply that there are no suitable PHP libraries.

Related

php - parsing html doc but issue with comparing text content [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I'm using PHP to retrieve a document and find some data within the HTML.
I've used Tidy clean and repair as the document contains lots of bad html.
Anyway,
In the html document there is a tag like:
Link 12345
I want to get the value of the attribute (www.google.com) if the text content (Link 12345) matches a certain string.
$h2 = $doc->getElementsByTagName('a');
for ($i2; $i2 < $h2->length; $i2++) {
$attr2 = $h2->item($i2)->getAttribute('href');
if ($h2->item($i2)->textContent == "Link 12345")
print "FOUND";
}
which doesn't seem to work. I know that the for loop returns 'Link 12345' at some point (when ->textContent is called). But the comparison always fails even though Link 12345 appears if it is printed out. I suspect there is some issue with the encoding but I can't get it fixed.
Thanks.
You can use PHP's DOMXPath to execute an XPath query against your DOM object.
I believe that for yours it'll be
//a[text()="Link 12345"]
Will return all the who's text is "Link 12345".
A simple bug: you are testing "$h2->item($i2)->textContent" instead of "$h2->textContent"
Isn't it?

Why can't I `print` an array in PHP? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm new to this, and I've looked on many different posts here, as well as on a lot of other resources found on google, but I'm still struggling with arrays.
in my html, I'm basically trying to display a sum total of a column of dollar amounts in my database.
Here's my HTML/PHP:
<div class="col-sm-2">
<h1><?php get_sum_income(); ?></h1>
</div>
and here's my external PHP file with the function it calls:
function get_sum_income() {
require(ROOT_PATH . "inc/database.php");
try {
$results = $db->prepare("SELECT SUM(cat_amount) FROM categories WHERE is_income = '1';");
$results->execute();
} catch (Exception $e) {
echo ("ERROR: Data could not be retrieved from the database." . $e);
exit;
}
$sum_income = $results->fetch(PDO::FETCH_ASSOC);
print $sum_income;
};
I keep getting an "array to string conversion" error, and I know it has to do with my understanding of arrays (or lack thereof). I've looked around, and I just need help with my specific situation.
Also, is how I'm accessing the function the best way to do this?
You can't print an array. print is for scalars. PHP is trying to convert your array to a string so that it can print it, but even its own attempt at this fails, because it's simply not supported.
You can print_r it, though!
Update Sounds like you want to retrieve, on its own, the single element of the array that is your database query result. It's in an array because PHP doesn't know ahead of time that your resultset will only have a single column in it.
In your case, you'd write:
print $sum_income['SUM(cat_amount)'];
This is pretty unwieldy, though. You can shorten it by giving the column an alias in your MySQL query:
SELECT SUM(`cat_amount`) AS `mySum` FROM `categories` WHERE `is_income` = '1';
Then:
$row = $results->fetch(PDO::FETCH_ASSOC);
print $row['mySum'];
(I've improved the variable name, too!)
You can't print or echo arrays. You can, however, print a specific element of it:
echo $myArray['foo'];
Or, you can var_dump or print_r it.

Dynamically replacing # and # like Twitter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For my Laravel-based site, I need to find # and # within text content and replace it with a URL, such as a URL pointing at a user's Twitter page. How can I:
reliably find these strings within text portions of the HTML
replace found instances with a URL
The code for it is vast. You will have to use ajax here, in the textarea/textbox you will have to use "onkeyup" event, every key pressed have to be compared with "#" or "#" then the next character right after "#" has to be searched in the database.
So lets saw the user has typed "#A" till now and the user aims to type "#Ankur" Then as soon as "A" is typed the ajax script will start searching for users in the database and it is retrieved with the name, url and you just have to echo it on the screen.
THis is what you are looking for.. https://stackoverflow.com/a/4277114/829533
$strTweet = preg_replace('/(^|\s)#(\w*[a-zA-Z_]+\w*)/', '\1#\2', $strTweet);
And https://stackoverflow.com/a/4766219/829533
$input = preg_replace('/(?<=^|\s)#([a-z0-9_]+)/i', '#$1', $input);
Using regex it's rather simple. I would make one function that takes a prefix and replacement, like so.
function tweetReplaceObjects($input, $prefix, $replacement) {
return preg_replace("/$prefix([A-Za-z0-9_-])/", $replacment, $input);
}
An example usage would be something like this.
$text = 'hey look, it\'s #stackoverflow over there';
// expected output:
// hey look, it's stackoverflow over there
echo tweetReplaceObjects($text, '#', '$1');

Make every word in a string into classes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to create an CMS-system where I can add photos to my website, and for the photo-part, I want to have the ability to enter which programs I had used to create that photo.
I should be possible to add more than one program.
Right now I am saving the programs in the database like this:
In the programs-row:
photoshop illistrator indesign
Then at my website, it could be nice if the icons/logos of the used programs, could show up next to the photo.
So my question is how to do create a new div, what a new class, based on the words from the programs-row?
Fx:
photoshop illustrator indesign
Becomes to:
<div class="photoshop"></div>
<div class="illustrator"></div>
<div class="indesign"></div>
Hope you guys can help me with this problem :)
Thanks ;)
Use the explode function and a foreach loop to perform the string manipulation.
$programs = explode(" ", $data);
foreach($programs as $value) {
//Echo out the html - $value contains the program name
}
I leave it to you to figure out how to format the program name with the HTML that you need.
$fx = "photoshop illistrator indesign";
$words = explode(" ", $fx);
array_walk($words, function(&$n) {
echo '<div class="'.$n.'"></div>';
});

In PHP, how can I use the DOMDocument class to replace the src= attribute of an IMG tag? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
It's often useful to be able to swap out the src= attribute of an HTML IMG tag without losing any of the other attributes. What's a quick, non-regex way of doing this?
The reasons I don't want to use RegEx are:
It's not very readable. I don't want to spend 20 mins deciphering a pattern every time I need to account for a new case.
I am planning on modifying this function to add in width and height attributes when they're missing. A simple RegEx string replacement won't be easy to modify for this purpose.
Here's the context: I have a bunch of RSS feed posts that each contain one image. I would like to replace these images with blank images, but keep the HTML otherwise unaffected:
$raw_post_html = "<h2>Feed Example</h2>
<p class='feedBody'>
<img src='http://premium.mofusecdn.com/6ff7098b3c8561d70c0af16d30e57d4e/cache/other/48da8425bc54af2d5d022f28cc8b021c.200.0.0.png' alt='Feed Post Image' width='350' height='200' />
Feed Body Content
</p>";
echo replace_img_src($raw_post_html, "http://cdn.company.org/blank.gif");
This is what I've come up with. It uses the PHP DOM API to create a tiny HTML document, then saves the XML for just the IMG element.
function replace_img_src($original_img_tag, $new_src_url) {
$doc = new DOMDocument();
$doc->loadHTML($original_img_tag);
$tags = $doc->getElementsByTagName('img');
if(count($tags) > 0)
{
$tag = $tags->item(0);
$tag->setAttribute('src', $new_src_url);
return $doc->saveHTML($tag);
}
return false;
}
Note: In versions of PHP before 5.3.6, $doc->saveHTML($tag) can be changed to $doc->saveXML($tag).

Categories