1 Replies - 454 Views - Last Post: 31 January 2012 - 02:14 PM Rate Topic: -----

#1 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 166
  • Joined: 21-November 11

Error received with regular expression

Posted 31 January 2012 - 01:04 PM

Title Correction:

NO error received with regular expression

Any ideas how I can access the numeric value for the currency rate from the following XML tag:

<description>1 British Pound Sterling = 1.20409 Euro</description>



I require the numeric value 1.20409. Another problem will be the number 1 before British Pound.


My attempt so far:

function get_rate1(SimpleXMLElement $xml) {
    $exchange['rate'] = $xml->channel->item[15]->description;
    echo $exchange['rate'] . "<br />";

    // Preg match regular expression to extract numbers
    $imgpattern = "/^([0-9])$/";
    preg_match($imgpattern, $exchange, $matches);

    $exchange['rate'] = $matches[1];
    echo $exchange['rate'] . ' /><br />';

    return $rate;
}




Nothing is displayed with the above?

This post has been edited by NewToJava2011: 31 January 2012 - 01:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Error received with regular expression

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,177
  • Joined: 08-August 08

Re: Error received with regular expression

Posted 31 January 2012 - 02:14 PM

Bookmark this site:
<?php
  # URL that generated this code:
  # http://txt2re.com/index-php.php3?s=%3Cdescription%3E1%20British%20Pound%20Sterling%20=%201.20409%20Euro%3C/description%3E&10

  $txt='<description>1 British Pound Sterling = 1.20409 Euro</description>';

  $re1='.*?';	# Non-greedy match on filler
  $re2='([+-]?\\d*\\.\\d+)(?![-+0-9\\.])';	# Float 1

  if ($c=preg_match_all ("/".$re1.$re2."/is", $txt, $matches))
  {
      $float1=$matches[1][0];
      print "($float1) \n";
  }

  #-----
  # Paste the code into a new php file. Then in Unix:
  # $ php x.php 
  #-----
?>

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1