Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help with PHP format Conversion

Posted on 2010-01-04
4
Medium Priority
?
323 Views
Last Modified: 2013-12-12
I have successfully parsed marquee value of a stock quote into this format.. Please suggest a way to convert this into a database with following mysql database table

Date | Time | CODE | CURRENTPRICE     <!--DELETE THIS PART (SCRAP bracket values ) (scrap price changes) -->

As of 2010-01-04   14:39:38     ABBL 380 ( 250 ) (  0 )      ACEDBL 385 ( 743 ) (  2 )      AFC 375 ( 500 ) (  0 )      AFL 400 ( 560 ) (  0 )      AHPC 560 ( 750 ) (  -20 )      BLDBL 472 ( 11 ) (  -9 )      BOAN 479 ( 410  ) (  15 )      BOK 925 ( 1,011 ) (  -5 )      CEDBL 780 ( 1,090 ) (  -10 )      CFL 397 ( 1,180 ) (  -5 )      CMBSL 300 ( 280 ) (  5 )      CZBIL 590 ( 1,490 ) (  0 )      DCBL 307 ( 1,373 ) (  -13 )      EBL 1,677 ( 50 ) (  -68 )      GBL 308 ( 180 ) (  -112 )      GDBL 499 ( 40 ) (  9 )      GFCL 332 ( 350 ) (  -6 )      GMFIL 555 ( 15 ) (  10 )      HBL 1,080 ( 182 ) (  -10 )      ICFC 370 ( 110 ) (  0 )      IDBL 386 ( 220 ) (  0 )      IFIL 329 ( 200 ) (  -6 )      ILFC 207 ( 682 ) (  -2 )      IMEFI 844 ( 100 ) (  -17 )      KBL 530 ( 895 ) (  -14 )      KIST 304 ( 6,525 ) (  -3 )      LBFIL 302 ( 20 ) (  4 )      LFLC 406 ( 45 ) (  -4 )      LGIL 188 ( 800 ) (  0 )      LICN 680 ( 10 ) (  0 )      LUBL 324 ( 1,220 ) (  -9 )      MBL 334 ( 200 ) (  -6 )      NABBC 465 ( 17 ) (  9 )      NABIL 2,597 ( 333 ) (  -73 )      NBB 290 ( 2,820 ) (  -1 )      NCCB 296 ( 160 ) (  -2 )      NCCBPO 206 ( 200 ) (  7 )      NCM 715 ( 47 ) (  0 )      NFS 456 ( 160 ) (  -9 )      NHMF 375 ( 240 ) (  -5 )      NIB 856 ( 692 ) (  -34 )      NICB 655 ( 192 ) (  -35 )      NLIC 1,000 ( 24 ) (  -20 )      NMB 330 ( 2,329 ) (  0 )      NSM 215 ( 220 ) (  4 )      NTC 500 ( 800 ) (  -2 )      PCBL 590 ( 1,070 ) (  -10 )      PFL 420 ( 150 ) (  -5 )      PRFL 475 ( 4,290 ) (  11 )      PSDBL 490 ( 320 ) (  -1 )      RMBFI 350 ( 435 ) (  -30 )      SBBL 571 ( 4,710 ) (  -8 )      SBI 873 ( 222 ) (  -17 )      SBL 470 ( 413 ) (  -10 )      SCB 3,434 ( 789 ) (  -61 )      SDBL 226 ( 500 ) (  -4 )      SIL 180 ( 80 ) (  0 )      SRBL 391 ( 10,100 ) (  -9 )      STFL 250 ( 1,581 ) (  -10 )      TBBL 693 ( 240 ) (  12 )      UFL 360 ( 980 ) (  -290 )      VBBL 373 ( 5,920 ) (  -2 )  


      
0
Comment
Question by:rotatingluficer
4 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26171212
You probably wan to use one field for the DATETIME value.

By "CODE" do you mean the name of the stock symbol?

What is the meaning of "DELETE THIS PART" - what do you want us to understand about that?

Thanks, ~Ray
0
 
LVL 32

Accepted Solution

by:
DrDamnit earned 2000 total points
ID: 26171365
Try this:
<?php
$h = mysql_connect('localhost','user','pass');
$db = mysql_select_db('test',$h);

function parse_array($string) {
        $parts = explode(' ',$string); //blow the line into parts.
        $r = array($parts[0],$parts[1]);
        return $r;
}



$pattern = '/[A-Z]*\s[0-9]*\s\(\s*[0-9]*\s*\)\s\(\s*[\s|-][0-9]*\s*\)/';
$string = 'As of 2010-01-04   14:39:38     ABBL 380 ( 250 ) (  0 )      ACEDBL 385 ( 743 ) (  2 )      AFC 375 ( 500 ) (  0 )      AFL 400 ( 560 ) (  0 )      AHPC 560 ( 750 ) (  -20 )      BLDBL 472 ( 11 ) (  -9 )      BOAN 479 ( 410  ) (  15 )      BOK 925 ( 1,011 ) (  -5 )      CEDBL 780 ( 1,090 ) (  -10 )      CFL 397 ( 1,180 ) (  -5 )      CMBSL 300 ( 280 ) (  5 )      CZBIL 590 ( 1,490 ) (  0 )      DCBL 307 ( 1,373 ) (  -13 )      EBL 1,677 ( 50 ) (  -68 )      GBL 308 ( 180 ) (  -112 )      GDBL 499 ( 40 ) (  9 )      GFCL 332 ( 350 ) (  -6 )      GMFIL 555 ( 15 ) (  10 )      HBL 1,080 ( 182 ) (  -10 )      ICFC 370 ( 110 ) (  0 )      IDBL 386 ( 220 ) (  0 )      IFIL 329 ( 200 ) (  -6 )      ILFC 207 ( 682 ) (  -2 )      IMEFI 844 ( 100 ) (  -17 )      KBL 530 ( 895 ) (  -14 )      KIST 304 ( 6,525 ) (  -3 )      LBFIL 302 ( 20 ) (  4 )      LFLC 406 ( 45 ) (  -4 )      LGIL 188 ( 800 ) (  0 )      LICN 680 ( 10 ) (  0 )      LUBL 324 ( 1,220 ) (  -9 )      MBL 334 ( 200 ) (  -6 )      NABBC 465 ( 17 ) (  9 )      NABIL 2,597 ( 333 ) (  -73 )      NBB 290 ( 2,820 ) (  -1 )      NCCB 296 ( 160 ) (  -2 )      NCCBPO 206 ( 200 ) (  7 )      NCM 715 ( 47 ) (  0 )      NFS 456 ( 160 ) (  -9 )      NHMF 375 ( 240 ) (  -5 )      NIB 856 ( 692 ) (  -34 )      NICB 655 ( 192 ) (  -35 )      NLIC 1,000 ( 24 ) (  -20 )      NMB 330 ( 2,329 ) (  0 )      NSM 215 ( 220 ) (  4 )      NTC 500 ( 800 ) (  -2 )      PCBL 590 ( 1,070 ) (  -10 )      PFL 420 ( 150 ) (  -5 )      PRFL 475 ( 4,290 ) (  11 )      PSDBL 490 ( 320 ) (  -1 )      RMBFI 350 ( 435 ) (  -30 )      SBBL 571 ( 4,710 ) (  -8 )      SBI 873 ( 222 ) (  -17 )      SBL 470 ( 413 ) (  -10 )      SCB 3,434 ( 789 ) (  -61 )      SDBL 226 ( 500 ) (  -4 )      SIL 180 ( 80 ) (  0 )      SRBL 391 ( 10,100 ) (  -9 )      STFL 250 ( 1,581 ) (  -10 )      TBBL 693 ( 240 ) (  12 )      UFL 360 ( 980 ) (  -290 )      VBBL 373 ( 5,920 ) (  -2 )';
$string = str_replace(',','',$string); //Remove the commas.

preg_match_all($pattern,$string,$matches);

$pattern = '/[0-9]{4}-[0-9]{2}-[0-9]{2}/'; //<--Extract the date
preg_match($pattern,$string,$date);
echo "Using date $date[0]i\n";

$pattern = '/[0-9]{2}:[0-9]{2}:[0-9]{2}/'; //<--Extract the time.
preg_match($pattern,$string,$time);
echo "Using time $time[0]\n";

$matches = $matches[0];
foreach($matches as $match) {
        $a = parse_array($match);
        printf("Code: %s \t Currentprice %s\n",$a[0],$a[1]);
        $sql = sprintf("INSERT INTO `table` (`date`,`time`,`code`,`currentprice`) VALUES('%s','%s','%s','%s')",$date[0],$time[0],$a[0],$a[1]);
        echo "SQL: $sql\n";
        $r = mysql_query($sql);
}
?>

Open in new window

0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 26171377
<?php

$stockquotestring = "As of 2010-01-04   14:39:38     ABBL 380 ( 250 ) (  0 )      ACEDBL 385 ( 743 ) (  2 )      AFC 375 ( 500 ) (  0 )      AFL 400 ( 560 ) (  0 )      AHPC 560 ( 750 ) (  -20 )      BLDBL 472 ( 11 ) (  -9 )      BOAN 479 ( 410  ) (  15 )      BOK 925 ( 1,011 ) (  -5 )      CEDBL 780 ( 1,090 ) (  -10 )      CFL 397 ( 1,180 ) (  -5 )      CMBSL 300 ( 280 ) (  5 )      CZBIL 590 ( 1,490 ) (  0 )      DCBL 307 ( 1,373 ) (  -13 )      EBL 1,677 ( 50 ) (  -68 )      GBL 308 ( 180 ) (  -112 )      GDBL 499 ( 40 ) (  9 )      GFCL 332 ( 350 ) (  -6 )      GMFIL 555 ( 15 ) (  10 )      HBL 1,080 ( 182 ) (  -10 )      ICFC 370 ( 110 ) (  0 )      IDBL 386 ( 220 ) (  0 )      IFIL 329 ( 200 ) (  -6 )      ILFC 207 ( 682 ) (  -2 )      IMEFI 844 ( 100 ) (  -17 )      KBL 530 ( 895 ) (  -14 )      KIST 304 ( 6,525 ) (  -3 )      LBFIL 302 ( 20 ) (  4 )      LFLC 406 ( 45 ) (  -4 )      LGIL 188 ( 800 ) (  0 )      LICN 680 ( 10 ) (  0 )      LUBL 324 ( 1,220 ) (  -9 )      MBL 334 ( 200 ) (  -6 )      NABBC 465 ( 17 ) (  9 )      NABIL 2,597 ( 333 ) (  -73 )      NBB 290 ( 2,820 ) (  -1 )      NCCB 296 ( 160 ) (  -2 )      NCCBPO 206 ( 200 ) (  7 )      NCM 715 ( 47 ) (  0 )      NFS 456 ( 160 ) (  -9 )      NHMF 375 ( 240 ) (  -5 )      NIB 856 ( 692 ) (  -34 )      NICB 655 ( 192 ) (  -35 )      NLIC 1,000 ( 24 ) (  -20 )      NMB 330 ( 2,329 ) (  0 )      NSM 215 ( 220 ) (  4 )      NTC 500 ( 800 ) (  -2 )      PCBL 590 ( 1,070 ) (  -10 )      PFL 420 ( 150 ) (  -5 )      PRFL 475 ( 4,290 ) (  11 )      PSDBL 490 ( 320 ) (  -1 )      RMBFI 350 ( 435 ) (  -30 )      SBBL 571 ( 4,710 ) (  -8 )      SBI 873 ( 222 ) (  -17 )      SBL 470 ( 413 ) (  -10 )      SCB 3,434 ( 789 ) (  -61 )      SDBL 226 ( 500 ) (  -4 )      SIL 180 ( 80 ) (  0 )      SRBL 391 ( 10,100 ) (  -9 )      STFL 250 ( 1,581 ) (  -10 )      TBBL 693 ( 240 ) (  12 )      UFL 360 ( 980 ) (  -290 )      VBBL 373 ( 5,920 ) (  -2 )   ";

$date = substr($stockquotestring,6,10);
$time = substr($stockquotestring,19,8);
$string1 = substr($stockquotestring,27);
$array1 = explode (" ", $string1);

for($i=0;$i<count($array1);$i++)
{
      if ($array1[$i] == null)
            {}
      elseif (substr($array1[$i],0) == "(")
            {$i=$i+2;}
      elseif (substr($array1[$i],0) == ")")
            {}
      else
      {
            $stockarray[] = $array1[$i];
            $stockarray[] = $array1[$i+1];
            $i++;
      }
}

// connect to your database here

for ($j=0;$j<count($stockarray);$j=$j+2)
{
      $code = $stockarray[$j];
      $currentprice = $stockarray[$j+1];
      mysql_query("INSERT INTO Table (Date, Time, CODE, CURRENTPRICE) VALUES('$date', '$time', '$code', $currentprice");
}

exit();
?>
0
 

Author Closing Comment

by:rotatingluficer
ID: 31672769
<?php
$txt = file_get_contents("http://www.nepalstock.com");

$arr = get_tag($txt, "marquee");
$out = strip_tags($arr[0]);
$output = str_replace('&nbsp;', '', $out);
//echo $output;

function get_tag($txt,$tag){
$offset = 0;
$start_tag = "<".$tag;
$end_tag = "";
$arr = array();
do{
$pos = strpos($txt,$start_tag,$offset);
if($pos){
$str_pos = strpos($txt,">",$pos)+1;
$end_pos = strpos($txt,$end_tag,$str_pos);
$len = $end_pos - $str_pos;
$f_text = substr($txt,$str_pos,$len);

$arr[] = $f_text;
$offset = $end_pos;
}
}while($pos);
return $arr;

}

//$h = mysql_connect('localhost','user','pass');
//$db = mysql_select_db('test',$h);

function parse_array($string) {
        $parts = explode(' ',$string); //blow the line into parts.
        $r = array($parts[0],$parts[1]);
        return $r;
}



$pattern = '/[A-Z]*\s[0-9]*\s\(\s*[0-9]*\s*\)\s\(\s*[\s|-][0-9]*\s*\)/';
$string = $output;

$string = str_replace(',','',$string); //Remove the commas.

preg_match_all($pattern,$string,$matches);

$pattern = '/[0-9]{4}-[0-9]{2}-[0-9]{2}/'; //<--Extract the date
preg_match($pattern,$string,$date);
echo "Using date $date[0]\n
";

$pattern = '/[0-9]{2}:[0-9]{2}:[0-9]{2}/'; //<--Extract the time.
preg_match($pattern,$string,$time);
echo "Using time $time[0]\n

";

$matches = $matches[0];
foreach($matches as $match) {
        $a = parse_array($match);
        printf("%s\n",$a[1]);
      //  $sql = sprintf("INSERT INTO `table` (`date`,`time`,`code`,`currentprice`) VALUES('%s','%s','%s','%s')",$date[0],$time[0],$a[0],$a[1]);
     //   echo "SQL: $sql\n";
     //   $r = mysql_query($sql);
     echo $a[0] . "
";
}
?>
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question