0

I have this HTML code:

<span class="price">Price: <font class="rosu_12px_bold">2'099<sup>99</sup></font> EUR</span>

and i managed to extract the price: 2'09999

I cant catch the sup tag and insert a .

Now, in my database, i can only store real numbers

The value extracted is not a number so i get the 0 value, when trying to insert this value.

I can only insert it this way: 2099.99

I can never know what my extracted value looks like, so i need a solution to format it.

3
  • 2
    Once you have the string in a variable, you will need to regex the data out of it. It's not going to be amazingly pretty though depending on how much variation there is in your data. Commented Aug 10, 2012 at 7:53
  • Remove all non digits and divide by 100. like @Fluffeh said, if there is variation than this won't work either. Commented Aug 10, 2012 at 7:54
  • i cant detect if <sup> tag is there Commented Aug 10, 2012 at 8:15

2 Answers 2

4

Try this:

<?php
    $abc="2'099";
    echo preg_replace('/[^0-9]/i', '', $abc);
    ?>

Edit: Remember this will replace period(.) as well.

Yet another edit: You can use strip tags to strip html tags

<?php
$abc="<font class='rosu_12px_bold'>2'099<sup>99</sup></font>";
echo preg_replace('/[^0-9.]/i', '', strip_tags($abc));
?>

Edit: But 99 between sup tags might denote the lower denomination of the currency may be 99 cents. In this case above example will not work.

Yet another edit, Before using strip tags, you can replace sup tag with period(.) and then use strip_tags, if you want to maintain the lower denomination of the currency.

Hope this helps

Sign up to request clarification or add additional context in comments.

13 Comments

If he is working with the full example HTML is stated this will not work since it will catch rosu_12px_bold as well, it is unclear as to whether it he is attempting to regex out HTML or not.
the thing is that the main app is a bookmarklet, and i use highlighter to select tags from DOM, and a special function extracts the values within the selected area, so i dont know what tags are in there, i only get the value; this is one partial solution, but i need the entire number, even the 99 cents are very important here. i think i got it, i will post the answer soon, i hope
@IonutFlaviusPogacian If the sup tag isnt there what would you use as the deliminator?
on every eShop, somehow, they print the product price, using also cents if price is like that; prices may be like 1.234,99 or 2'345.99 or 3'456<sup>99</sup> and my problem is that i have to catch 1234.99 instead of 123.456; 1234.99 is the right number to insert in the database
@IonutFlaviusPogacian Do an if statement to isolate using the a preg_match for <sup> then run one of the answers, that will create the full solution, prolly easier than trying to make a CPU heavy regex to do an OR
|
0

I only get strings like 1'23499 or 1.99989 or 1'345'99 or 1'234.99 or other formats.

The issue is how to get the 99 cents?

Involved in these values are the following chars:

0 1 2 3 4 5 6 7 8 9 . , '

in any combination possible

My solution is:

  1. Replace everything which is not a number 0 - 9 with . (dot)

  2. Parse the string, from position 0 to strlen-1 and from where the first . (dot) is found, from 3 to 3 characters try and insert a . (dot). If the end of the string has been reached, avoid inserting .

  3. Find position of last . (dot) inserted and remove all the other . (dot)s .

The Result should be the number 1234.99.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.