Question

I curl and get the following:

echo $contents

which gives me this:

 <td class="fomeB1" >Balance: $ 1.02</td>

$13.32 fee
$15.22 fee 2

How do I just grab the 1.02 - everything after the $ and in front of the </td>

I want to strip this via php and put the money into variable $balance....

Any kind of help I can get on this is greatly appreciated!

Was it helpful?

Solution

yes you can use exactly what u want

preg_match("/(?<=Balance: ).*?(?=<)/", "<td class='fomeB1'>Balance: $ 1.02</td>", $match);
print_r(str_replace("$"," ",$match));

// Prints:
Array
(
    [0] =>   1.02
)

OTHER TIPS

This is probably a bad way of doing it... but

$pieces = explode("$ ", $contents);
$pieces = explode("</td>", $pieces[1]);
$balance = $pieces[0];

Or you can use a regular expression. Something like this:

\$\s\d+.{1}\d+

You can test the regular expression here: RegExpPal

You can use preg_match() to parse the balance using the regular expression. preg_match()

Basically now you have a string

$str = '<td class="fomeB1" >Balance: $ 1.02</td>';

Am I right ?

now try this :

$txt = getTextBetweenTags($str, "td");
echo $txt;//Balance: $ 1.02

Now, use explode:

$pieces = explode($txt,' $ ');
echo $pieces[0]; //Balance:
echo $pieces[1]; //1.02

UPDATE: try this, it should work if explode works for a string:

$pieces = explode("Balance: $ ", $str);
$pieces = explode("</td>", $pieces[1]);
$balance = $pieces[0]; //1.02
$str = '<td class="fomeB1" >Balance: $ 1.02</td>';  
$r1 = preg_replace('/.*?\$[ ](.*?)<.*/', '\1',  $str); //match between after first '$ ' ,and first '<' ;
echo $r1; //1.02

or

$r2= preg_replace('/^<td class="fomeB1" >Balance\: \$ (.*?)<\/td>$/', '\1',  $str); //match between after <td class="fomeB1" >Balance: $    and </td>

echo $r2; //1.02

or update

$str = ' <td class="fomeB1" >Balance: $ 1.02</td>

$13.32 fee
$15.22 fee 2';  

$r1 = preg_replace('/.*?\$[ ](.*?)<.*/s', '\1',  $str); //match between after first '$ ' ,and first '<' ;
echo $r1.'<br>'; //1.02
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top