JohnDotOwl
JohnDotOwl

Reputation: 3755

Simple HTML Parser

<strong class="tb-rmb-num"><em class="tb-rmb">¥</em>39.00</strong>

I'm trying to retrieve the number only without the currency sign

My current code is

$ret = $html->find('strong[class=tb-rmb-num]');
echo $ret[0];

This will retrieve it with the sign ¥39.00 Advice please, Thank you.

Upvotes: 0

Views: 134

Answers (1)

Drew
Drew

Reputation: 4373

In php:

$string = '¥39.00';

if(preg_match('/([\d\.]+)/', $string, $m)){
    echo $m[1];
} 

Which outputs:

39.00

Ok, I will break this down:

preg_match('/([\d\.]+)/', $string, $m)

preg_match is a php function that allows us to look for pattern matches in a given string using regular expressions.

The regular expression in this case is: /([\d.]+)/

  • The / .. / is the delimeter that contains the expression
  • The ( .. ) is a group, which whatever match is found inside the group is stored in $m. We only have one group (as in only have one set of parenthesis) so this frist group match is retrived via $m[1]
  • The [ .. ] is a character class.
  • The \d is a shortcut for all digit characters, 0-9
  • The . means a literal . character (it is escaped with the \ because a . in a regular expresion has special meaning but we want it to represent a literal . and not it's special meaning)
  • the + after the character class [..] means match the characters in the class as many times in a row as it can

The value of $string in this example was set to ¥39.00. You will want to replace $string in my example with your $ret[0] instead.

$m is a variable placeholder to store our group matches (explained above)

The whole thing was wrapped in an if statement so you can do something if the pattern match was found else do something else if it wasn't.

For further reference:

  1. http://php.net/manual/en/function.preg-match.php
  2. http://webcheatsheet.com/php/regular_expressions.php

Upvotes: 3

Related Questions