Page 1 of 1

unsure of how too make this work

Posted: Wed Feb 14, 2007 1:01 pm
by afbase
I am trying to create a Regex that will remove the "+" and "%" and select the number in the first example or simply just the "%" in the second example.
first example:

Code: Select all

<td>Company</td><td><span class="s4">+3.40%</span>
Second Example:

Code: Select all

<td>Company</td><td><span class="s5">-201.30%</span>


So far this is what I have for my regex:

Code: Select all

function mysql_past_5_earnings($ticker){
        $url = "http://moneycentral.msn.com/investor/invsub/analyst/earnest.asp?Page=EarningsGrowthRates&Symbol=".$ticker;
        $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_FAILONERROR, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        $result = curl_exec($ch);
                curl_close($ch);
        $pattern = '!<td>Company</td><td><span\s*class="s4">([^<]*)</span>!';
        preg_match($pattern,$result,$earnings);
        $this->past_5_earnings = preg_replace('#(+[^.]*).(^%*)|([^.]*).([^%]*)#', '$1$2', $earnings[1]);
}

The error returned from execution is:
Warning: preg_replace(): Compilation failed: nothing to repeat at offset 1 in /home/clinton/hdd1/coldowl/project/stock/tests/graham.php on line 190
Any help is appreciated :)

Posted: Wed Feb 14, 2007 3:46 pm
by GeertDD
Here's the regex I came up with, just by looking at the given example strings. The parenthese capture the number you want.

Code: Select all

(-?\d+\.\d+)%