how can i retrieve 108 from the fallowing sample html using preg_mtch_all?

<span class="amount regular">
<span class="currency">$</span>
108
<sup></sup>
</span>
vishnu kalakotaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Have you tried the standard PHP solution of strip_tags() and ltrim()?  Regular expressions are often hard to get right, especially for the edge cases, and when we have only one data sample, the regular expression we write is almost always wrong for general application.

If you're new to PHP and want to learn the language, this article can guide you to some good learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
Ray PaseurCommented:
This seems to isolate the number correctly.
<?php // demo/temp_vishnu.php
/**
 *
 * http://www.experts-exchange.com/questions/28690491/how-can-i-retrieve-108-from-the-fallowing-sample-html-using-preg-mtch-all.html
 *
 * http://php.net/manual/en/ref.strings.php
 *
 */
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';

$txt = <<<EOD
<span class="amount regular">
<span class="currency">$</span>
108
<sup></sup>
</span>
EOD;


function isolate_number($str)
{
    $str = trim(strip_tags($str));
    $str = ltrim($str, '$');
    return $str;
}


echo isolate_number($txt);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Here are some other ways of looking at the problem.  There is no one "right" way; it's like arithmetic, where 6+3=9 and 5+4=9, etc.  The only thing I can tell you for sure is that with regular expressions you have something like 3+X=9 and you're never quite sure about the value of X until you deploy the application - then your assumptions are tested, because they are untestable before deployment, and we don't want that!
http://iconoun.com/demo/temp_vishnu.php

<?php // demo/temp_vishnu.php
/**
 *
 * http://www.experts-exchange.com/questions/28690491/how-can-i-retrieve-108-from-the-fallowing-sample-html-using-preg-mtch-all.html
 *
 * http://php.net/manual/en/ref.strings.php
 *
 */
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';


$txt = <<<EOD
<span class="amount regular">
<span class="currency">$</span>
108
<sup></sup>
</span>
EOD;


function isolate_number($str)
{
    $str = trim(strip_tags($str));
    $str = ltrim($str, '$');
    return trim($str);
}

$new = isolate_number($txt);
var_dump($new);


$rgx
= '#'              // REGEX DELIMITER
. '['              // START CHARACTER CLASS
. '^'              // NEGATION - MATCH NONE OF THESE
. '0-9.'           // NUMBERS, INCLUDING WESTERN DECIMAL POINT
. ']'              // END OF CHARACTER CLASS
. '#'              // END OF REGULAR EXPRESSION
;

$new = preg_replace($rgx, NULL, $txt);
var_dump($new);


$rgx
= '#'              // REGEX DELIMITER
. '('              // START OF CAPTURE GROUP
. '['              // START CHARACTER CLASS
. '0-9.'           // NUMBERS, INCLUDING WESTERN DECIMAL POINT
. ']'              // END OF CHARACTER CLASS
. ')'              // END OF CAPTURE GROUP
. '#'              // END OF REGULAR EXPRESSION
;

preg_match_all($rgx, $txt, $mat);
$new = implode(NULL, $mat[1]);
var_dump($new);
echo PHP_EOL;

Open in new window

And here is an excellent (tongue-in-cheek) explanation of why we don't use regular expressions to parse markup:
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?answertab=oldest#tab-topdocuments.
0
Julian HansenCommented:
Using preg_match_all
<pre>
<?php
$string = <<< STRING
<span class="amount regular">
<span class="currency">$</span>
108
<sup></sup>
</span>
STRING;
$pattern = '/<span class="currency">\$<\/span>\s*(\d+)[\S\s]*?<\/span>?/';
$result = preg_match_all($pattern, $string, $results);
print_r($results);
?>
</pre>

Open in new window

Output
Array
(
    [0] => Array
        (
            [0] => $
108


        )

    [1] => Array
        (
            [0] => 108
        )

)

Open in new window

0
vishnu kalakotaAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.