1

Forgive me as I am a newbie programmer. How can I assign the resulting $matches (preg_match) value, with the first character stripped, to another variable ($funded) in php? You can see what I have below:

<?php
$content = file_get_contents("https://join.app.net");

//echo $content;

preg_match_all ("/<div class=\"stat-number\">([^`]*?)<\/div>/", $content, $matches);
//testing the array $matches

//echo sprintf('<pre>%s</pre>', print_r($matches, true));

$funded = $matches[0][1];

echo substr($funded, 1);
?>
Wiseguy
  • 20,522
  • 8
  • 65
  • 81
  • I'm not sure what you are asking. It might help if you provide some sample input and sample desired output. – Trott Aug 11 '12 at 01:35

2 Answers2

0

I am not 100% sure but it seems like you are trying to get the dollar amount that the funding is currently ?

And the character is a dollar sign that you want to strip out ?

If that is the case why not just add the dollar sign to the regex outside the group so it isn't captured.

/<div class=\"stat-number\">\$([^`]*?)<\/div>/

Because $ means end of line in regex you must first escape it with a slash.

0

Don't parse HTML with RegEx.

The best way is to use PHP DOM:

<?php
$handle = curl_init('https://join.app.net');
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$raw = curl_exec($handle);
curl_close($handle);
$doc = new DOMDocument();
$doc->loadHTML($raw);
$elems = $doc->getElementsByTagName('div');
foreach($elems as $item) {
    if($item->getAttribute('class') == 'stat-number')
        if(strpos($item->textContent, '$') !== false) $funded = $item->textContent;
}
// Remove $ sign and ,
$funded = preg_replace('/[^0-9]/', '', $funded);
echo $funded;
?>

This returned 380950 at the time of posting.

Community
  • 1
  • 1
uınbɐɥs
  • 7,236
  • 5
  • 26
  • 42