Need help in DOM traverse using php [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to scrape the content post of this forum https://forum.lowyat.net/topic/3424996 using below code.
$rows = $html->find('.post_table');
$array = array();
foreach($rows as $go){
$post_text = $go->find('.post_td_right > .post_text')->innertext;
$array[]= array(
'content'=> $post_text
);
}
echo json_encode($array);
I var_dump($rows) and it's an object, I really don't know why is the mistake. Need your help!

Forums usually have an RSS feed to help with this sort of requirement. Turns out, the site you're scraping supplies this for you: http://rss.forum.lowyat.net/topic/3424996
We can now use an XML parser instead of a DOM scraper, which will be much more efficient. For example;
<?php
$rss = file_get_contents('http://rss.forum.lowyat.net/topic/3424996'); //Or use cURL
$xml = simplexml_load_string($rss);
$array = array();
foreach($xml->channel->item as $posts) {
$post = (array) $posts->description;
$array[] = htmlentities($post[0]);
}
echo "<pre>";
echo print_r($array);
echo "</pre>";

Related

Formatting multilevel JSON data in PHP from remote file [SOLVED] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I am trying to extract json data from a remote php file (not a json file) so that it each time the page is propagated it pulls the newest data to format. Here are examples of what I am trying to accomplish but I cant get it to work.
resc_payouts.php
{"number":1,"username":"ivalenel"},{"number":2,"username":"newb-6763"},{"number":3,"username":"jeremyhipps"},{"number":4,"username":"dcollier200"},{"number":5,"username":"AdrianEric311"},{"number":6,"username":"Prxnce24"},{"number":7,"username":"Bungy2004"},{"number":8,"username":"sevyf7"},{"number":9,"username":"jerocker79"},{"number":10,"username":"Djmoonknight8"},{"number":11,"username":"marcel_g_l"},{"number":12,"username":"zebuss"},{"number":13,"username":"fourZer0"},{"number":14,"username":"himalayabpatel"},{"number":15,"username":"Chip1234"},{"number":16,"username":"AsvpJ9k"},{"number":17,"username":"himmy23"},{"number":18,"username":"Chip1234"},{"number":19,"username":"Clares20"},{"number":20,"username":"ballermoss"},{"number":21,"username":"gareagan04"},{"number":22,"username":"cweatherfordinc"}
jsontest.php
<?php
$content = file_get_contents('https://**********.com/manage/resc_payouts.php');
$decoded_json = json_decode($content, true);
foreach($decoded_json as $key => $value) {
$username = $decoded_json[$key]["username"];
echo $username;
}
?>
I have tried many methods, converting to a string, encoding and then decoding but cant seem to figure this out. Any help on getting this remote data off a php file and formatting it would be immensely useful.
So after a few hours i found a solution
SOLUTION FOUND
<?php
$content = file_get_contents('https://**********.com/manage/resc_payouts.php');
$json = json_decode($content, TRUE); // decode the JSON into an associative array
foreach ($json as $key => $value) {
echo $value['number'];
echo $value['username'];
echo "<br/>";
}
?>
Thanks but i figured this out myself over time

Replace character depend on the data [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
$defaultdata = "abcdef00000000000000000000000000";
$data1 = "271";
$output = "abcdef00000000000000000000000271";
How can I replace the string based on the data. For example, if the default data is abcdef00000000000000000000000000 , so it will replace when the data1 got value. So the output will be abcdef00000000000000000000000271. How can I do this?
I'd use substr:
$output = substr($defaultdata,0,strlen($defaultdata)-strlen($data1)) . $data1;
Applying it to your code
<?php
$defaultdata = "abcdef00000000000000000000000000";
$data1 = "271";
$output = substr($defaultdata,0,strlen($defaultdata)-strlen($data1)) . $data1;
echo $output;
?>
$output = substr($defaultdata, 0, strlen($defaultdata) - strlen($data1));
$output .= $data1
I think the easiest way to solve this is to use str_pad. It is a built in function made to tackle situations like yours. Then you can easly swap defaultdata to something other. The code is as follow:
$output = str_pad($data1, strlen($defaultdata), $defaultdata, STR_PAD_LEFT);

How to split MySQL entry into different lines using PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Using simple HTML textarea I added some records in MySQL. While adding the entries I pressed enter to separate the lines. Now I want to select/display those records using PHP in bullets. I want each lines to be displayed into bullets.
E.g (the things I have added)
Name
Age
Gender
What I want
Name
Age
Gender
I am using following PHP script
$result = mysql_query("SELECT * FROM students");
echo "<ul>";
while($row = mysql_fetch_array($result))
{
echo '<li'.$row["details"].'</li>';
}
echo "</ul>";
While i'm not sure it's a good practice (what if the users forgot to use a newline/enter?) you can use the explode() function.
From the manual:
Returns an array of strings, each of which is a substring of string
formed by splitting it on boundaries formed by the string delimiter.
$listItems = explode("\n\n", $row['details']);
echo '<ul'>;
foreach($listItems as $item){
echo '<li>'. $item .'</li>';
}
echo '</ul>';
Please note: You're using mysql_ which is deprecated. Consider using PDO.
This should do the trick:
//assuming $result variable holds the db value from the textarea
$lines = explode("\n", $result);
echo "<ul>;
array_walk($lines, function($line) {
$sanitizedLine = htmlentities($line);
echo "<li>$sanitizedLine</li>";
});
echo "</ul>

How to get text from a stat table on another website [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to scrape text from this page: http://blues.nhl.com/club/player.htm?id=8455710, specifically the number in the "Win" category and the "OT" category, then take the numbers I get, multiply the Win number by 20,000, and the OT by 10,000, add them together and display the result.
The reason I'm doing this is because the goalie that the stats are for (Marty Brodeur) is getting paid a bonus of $10,000 per point he earns the team in goal, so 20K for a win (2pts) and 10K for a loss (1pt).
I'm thinking the code would be something along the lines of.
<?php
$get_file_contents( "http://blues.nhl.com/club/player.htm?id=8455710" );
$item ['wins'] = (path-to-object);
$item ['OT'] = (path-to-object);
$item ['wins'] * 20,000 = $item ['win_bonus'];
$item ['OT'] * 10,100 = $item ['OT_bonus'];
$item ['win_bonus'] + $item ['OT_bonus'] = $item ['bonus'];
?>
<?php echo( '<h2>$item['bonus']</h2>'); ?>
You can use PHP Simple HTML DOM to parse the URL and then locate the XPath (you can find the XPath using Chrome debugging and selecting the item you want.
Download the Simple HTML DOM PHP file from Here and then use the following PHP Code:
<?php
include 'simple_html_dom.php';
$page = file_get_html('http://blues.nhl.com/club/player.htm?id=8455710');
$win = $page->find('//*[#id="wideCol"]/div[4]/div/div/table[1]/tbody/tr[2]/td[3]', 0)->plaintext;
$OT = $page->find('//*[#id="wideCol"]/div[4]/div/div/table[1]/tbody/tr[2]/td[5]', 0)->plaintext;
echo("Win: " . $win . PHP_EOL);
echo("OT: " . $OT . PHP_EOL);
?>

Extract content of meta element 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 am totally new to PHP development and I would like to extract the contents of a meta tag.
I have this code that allows me to extract the contents of the element # squad.
// Pull in PHP Simple HTML DOM Parser
include("simplehtmldom/simple_html_dom.php");
// Settings on top
$sitesToCheck = array(
// id is the page ID for selector
array("url" => "http://www.arsenal.com/first-team/players", "selector" => "#squad"),
array("url" => "http://www.liverpoolfc.tv/news", "selector" => "ul[style='height:400px;']")
);
$savePath = "cachedPages/";
$emailContent = "";
// For every page to check...
foreach($sitesToCheck as $site) {
$url = $site["url"];
// Calculate the cachedPage name, set oldContent = "";
$fileName = md5($url);
$oldContent = "";
// Get the URL's current page content
$html = file_get_html($url);
// Find content by querying with a selector, just like a selector engine!
foreach($html->find($site["selector"]) as $element) {
$currentContent = $element->plaintext;;
}
// If a cached file exists
if(file_exists($savePath.$fileName)) {
// Retrieve the old content
$oldContent = file_get_contents($savePath.$fileName);
}
// If different, notify!
if($oldContent && $currentContent != $oldContent) {
// Build simple email content
$emailContent = "Hey, the following page has changed!\n\n".$url."\n\n";
}
// Save new content
file_put_contents($savePath.$fileName,$currentContent);
}
// Send the email if there's content!
if($emailContent) {
// Sendmail!
mail("me#myself.name","Sites Have Changed!",$emailContent,"From: alerts#myself.name","\r\n");
// Debug
echo $emailContent;
}
But I want to change this code to get the number of comments in income.
Here is the meta tag where i would just extract the number of comments :
<meta item="desc" content="Comments:645">
Am I clear enough, do you understand me?
If I am not explicit enough, ask me?
Thanks for help
There's two ways to do this. You could either use the native PHP function: get_meta_tags() like so:
$tags = get_meta_tags('http://yoursite.com');
$comments = $tags['desc'];
Or you could use RegEx, but the above would be much more practical.
What you are looking for might be screen scraping.
This is the process where a programming-language like php, python or ruby loads a website in memory and uses various selectors to grab content from it.
Screen scraping is mostly used on websites that feature a lot of interesting data but have no json or xml API's
having googled around for it I stumbled on this post:
PHP equivalent of PyQuery or Nokogiri?
This article explains more about screen-scraping for web:
http://en.wikipedia.org/wiki/Web_scraping
Look for use domDocument
$dom = new domDocument;
$dom->loadHTML($htmlPage);
$metas = $dom->documentElement->getElementsByTagName('meta');
$ar = array();
foreach ($metas as $meta) {
$name = $meta->getAttribute('name');
$value = $meta->getAttribute('content');
$ar[$name] = $value;
}
print_r($ar); // print array meta-values

Categories