How to get text from a stat table on another website [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 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);
?>

Related

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>

Need help in DOM traverse 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 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>";

Display 10 array cards [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
http://plnkr.co/edit/06lu6r34eGNfRq1fygNy
I want to only display 10 "cards" from my array, how would I do so?
I tried using unset() but it is very inefficient and doesn't even work for multiple cards.
Also how can I make the code display the "cards" horizontally instead of vertically?
If you’d like to use unset, the correct syntax would be unset($cards[1]); unset($cards[2]); unset($cards[3]); ... and so on.
However, for your particular situation, I’ll recommend you to use array_slice:
$cards = array(
"Messi", "Ronaldo", "Ibrahimovic", "Ribery", "Robben", "Neymar", "Rooney", "Casillas",
"Falcao", "Van Persie", "Hazard", "Iniesta", "Xavi", "Schweinsteiger", "Silva", "Fabregas",
"Lahm", "Aguero", "Cavani", "Vidic", "Ozil", "Mata", "Bale", "ThiagoSilva",
"Kompany", "Tevez", "Toure", "Ramos", "Suarez", "Pirlo", "DiMaria", "Neuer",
"Pique", "Buffon", "Lewandowski", "Gomez", "Chiellini", "Cole", "Pedro", "Busquets",
"Cech", "Muller", "Hummels", "Alonso", "Navas", "Modric", "Cazorla", "Gotze",
"Benzema", "Vidal", "Lavezzi"
);
shuffle($cards);
$cards = array_slice($cards, 0, 10);
For the horizontal display, you may simply omit the <br> at the end of every iteration of your loop, but, depending on the resolution of the user’s browser, the images may occupy more than one line. For a strictly horizontal arrangement, use an HTML table with 10 columns:
print("<table><tr>");
foreach($cards as $card){
$img = "http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/".$card.".png";
print("<td><img src=\"".$img."\"/></td>");
}
print("</tr></table>");
The following works too:
for ($i = 0; $i < 10; $i++) {
echo $cards[$i] ." <br>"; // put the name above the card
echo "<img src='http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/$card.png'> <br>";
}
If you want them horizontally, and without names, don't put in the <br>:
for ($i = 0; $i < 10; $i++) {
echo "<img src='http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/$card.png'>";
}

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