DOM+Xpath > Regex:
<?php
$str = '
<div id=currency_converter_result>1 USD = <span class=bld>46.4400 INR</span>
<input type=submit value="Convert">
</div>';
$d = new DOMDocument();
$d->loadHTML( $str );
$x = new DOMXpath($d);
$xpr = $x->evaluate('//span[contains(@class, "bld")]');
if ( count( $xpr ) ) {
foreach ( $xpr as $el ) {
echo $el->nodeValue;
}
}
?>
Of course feel free to use simplexml
or other similar libraries that involve less code.
Example of the chosen answer breaking, if the HTML was altered as Milan suggested:
<?php
$subject = '
<div>
<div id=currency_converter_result/><b/>1 USD = <span class=bld one>46.4400 INR</span>
<input type=submit value="Convert">
</div></div><span/>';
$pattern = '/<div id=currency_converter_result>.*?<span.*?>(.*?)<\/span>/';
preg_match($pattern, $subject, $matches);
print_r($matches); // output is Array ( )
Other regex answer breaking:
<?php
$subject = '
<div>
<div id=currency_converter_result/><b/>1 USD = <span class=bld one>46.4400 INR</span>
<input type=submit value="Convert">
</div></div><span/>';
preg_match('#<span class=bld>(.*?)</span>#', $subject, $match);
$value = $match[1];
var_dump($value); // outputs NULL
My DOM/Xpath solution works perfectly with the altered markup:
<?php
$subject = '
<div>
<div id=currency_converter_result/><b/>1 USD = <span class=bld one>46.4400 INR</span>
<input type=submit value="Convert">
</div></div><span/>';
$d = new DOMDocument();
$d->loadHTML( $subject );
$x = new DOMXpath($d);
$xpr = $x->evaluate('//span[contains(@class, "bld")]');
if ( count( $xpr ) ) {
foreach ( $xpr as $el ) {
echo $el->nodeValue; // output 46.4400 INR
}
}
?>