hey guy,i'm a newbie in php and need some help from you guy in this preg_match_all function.
i would like to grab the multiple share data in this webpage for change,last trade,volume,trade time,open and so on.

this code only able to grab a single line of data, but not multiple as i mention above.
anyone can provide some guidance using this function?

<?php
$homepage = file_get_contents("http://finance.yahoo.com/q/ta?s=4707.KL+Basic+Tech.+Analysis&t=3m") ;
 if ($homepage === false)
  {   echo "content not found";
	}
 else { 
      preg_match("/<span id=\"yfs_l10_4707.kl\">(.*)<\/span>/", $homepage, $array) ;
      $nestle = $array[1];	
	}
				
        ?>

download simple_html_dom.php:
http://simplehtmldom.sourceforge.net/

then try:

<?php
include("simple_html_dom.php");
$html=file_get_html("http://http://finance.yahoo.com/q/ta?s=4707.KL+Basic+Tech.+Analysis&t=3m");
$rows=$html->find('div[id=yfi_quote_summary_data] table tr');
$result=array();
foreach($rows as $row){
	preg_match('#([^:]+):(.+)#',strip_tags($row),$m);
	$result[]=array("label"=>$m[1],"value"=>$m[2]);
}
print_r($result);
?>

hey, i tried several time, the code seems like not working...
is it a mistake here?

("http://http://finance.yahoo.com/q/ta?s=4707.KL+Basic+Tech.+Analysis&t=3m");

i do ammend the http:// to one only...but seems like it still not working

It should be:
http://finance.yahoo.com/q/ta?s=4707.KL+Basic+Tech.+Analysis&t=3m

Are you sure it is including the file? Change:

include("simple_html_dom.php");

to:

require_once("simple_html_dom.php");

If it throws an error then you may have the wrong path to the file.

does this consider as correct output? Array ( [0] => Array ( [label] => Last Trade [value] => 43.80 ) [1] => Array ( [label] => Trade Time [value] => Nov 4 ) [2] => Array ( [label] => Change [value] => 0.10 (0.23%) ) [3] => Array ( [label] => Prev Close [value] => 43.90 ) [4] => Array ( [label] => Open [value] => 44.00 ) [5] => Array ( [label] => Bid [value] => 43.70 ) [6] => Array ( [label] => Ask [value] => 43.80 ) [7] => Array ( [label] => 1y Target Est [value] => N/A ) [8] => Array ( [label] => Day's Range [value] => 43.80 - 44.00 ) [9] => Array ( [label] => 52wk Range [value] => 32.40 - 45.00 ) [10] => Array ( [label] => Volume [value] => 1,600 ) [11] => Array ( [label] => Avg Vol (3m) [value] => 38,707 ) [12] => Array ( [label] => Market Cap [value] => N/A ) [13] => Array ( [label] => P/E (ttm) [value] => N/A ) [14] => Array ( [label] => EPS (ttm) [value] => N/A ) [15] => Array ( [label] => Div & Yield [value] => N/A (N/A) ) )

does this consider as correct output?

Yes, of course! It clearly shows you that $result IS an array with all the information you want. For example: echo $result[0]['label'] . ': '. $result[0]['value']; will give you the Last Trade information. If you want to see all the information, use a foreach:

foreach($result as $item)
{
  echo $item['label'] . ': '. $item['value'];
}

Yes, of course! It clearly shows you that $result IS an array with all the information you want. For .....

hey, hielo, if we want to insert a line break, why can't we write like this? it's keep consist of error.

foreach($result as $item)
{
  echo "$item["label"] . ": " . $item["value"]<br />" ;
	}

online tutorial example

foreach( $employeeAges as $key => $value)
{
	echo "Name: $key, Age: $value <br />";
}

You can use:

echo $item["label"] . ": " . $item["value"] . "<br />" ;

OR:

echo "{$item['label']} :  {$item['value']} <br />" ;

(the {} forces the engine to evaluate a variable that is wrapped/embedded in a string.)

commented: Nice answered hielo. thz guy +2

(the {} forces the engine to evaluate a variable that is wrapped/embedded in a string.)

oh,thanks a lot, it works !

btw,is it possible for us to get the content in the top gainer/loser section?
http://biz.thestar.com.my/marketwatch/

how we consider whether the content of website can be grabbed or not?

Line 4 of my FIRST post above has: $rows=$html->find('div[id=yfi_quote_summary_data] table tr'); which states: "within <div id='yfi_quote_summary_data'> locate all TABLE elements and within that every TR".

So you need to look at the markup you are working with and decide/determine what portions of HTML you are interested in. Look at the examples posted at the simplehtmldom page.

hmm....i see...
by the way, hielo,
can we retrieve the top volume/gainers/loser like using iframe or any other method?

http://biz.thestar.com.my/marketwatch/

look at the HTML of the link above. You will notice that the information you want is in a <TABLE> .

So, from the tool I referred you to, you need to use: $html->find('table', SOME_INDEX) Now, I ask you, starting from the top, is THAT table (the one that contains the information you want) the FIRST table in the document? The SECOND? etc.

Whatever your answer is you need to subtract one and replace SOME_INDEX with the number you get (after subtracting one).

The reason for that is that find() will return an array, so the first table will be at index 0, the second table at index 1, etc.

Once you have the right table print it's innertext:

echo $html->find('table', YOU_FIGURE_OUT_THE_NUMBER_THAT_GOES_HERE)->innertext;

From the markup, it seems that all the info you want is within <B> tags, so you can refine your search further:

$data = $html->find('table', YOU_FIGURE_OUT_THE_NUMBER_THAT_GOES_HERE)->find('b')->innertext;

//now use a foreach to see what is in $data

ok

From the markup, it seems that all the info you want is within <B> tags, so you can refine your search further:

ok,i was able to obtain the whole table,

echo $html->find('table', 0)->innertext;

but the table is not listing accordingly due to 1 line of code have been left out which is

<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=3 WIDTH=95% STYLE="font-family:verdana, Arial, Helvetica; font-size:9pt"></table>

what we need is this code right?

When you execute echo $html->find('table', YOU_FIGURE_OUT_THE_NUMBER_THAT_GOES_HERE)->innertext; it will give you everything INSIDE that table tag (NOT including the table tag itself). If you are getting everything inside, then it is working fine. You can add YOUR own opening and closing table tags: echo '<table id="gainersAndLosers">' . $html->find('table', YOU_FIGURE_OUT_THE_NUMBER_THAT_GOES_HERE)->innertext . "</table>";

hey,the code works perfectly! :)
by the way, is it possible for us to ammend the data we grab in innertext?
likewise,

<td align=LEFT><a href="s.asp?c=7164">KNM</a></td>

and i would like to rip off the "href" inside

<td align=LEFT>KNM</a></td>

is that possible?

use the strip_tags function:
http://php.net/manual/en/function.strip-tags.php

thanks,hielo! that strip_tags function is exactly what i need.
you are great, how you know every single function of PHP? :P

by the way, how we build an expandable sector just like "user contributed note" as php.net? any jargon for that?
i tried google search "expandable function php" but end up with nothing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.