This question has already been answered:

Example string: from 5 276 rub.

You need a template for preg_replace , for the number 5276.

Also please take into account that besides the line itself there may be different html code.

Reported as a duplicate by Akina , Denis Bubnov , rjhdby , aleksandr barakin , ߊߚߤߘ 12 Feb '17 at 15:40 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    "I also ask you to take into account that besides the line itself there may be different html code" - it still doesn’t explain anything. There may be other similar constructions there, for example, one more number, framed by the words "from" and "rub"? Somehow can all this be framed by a tag, which is unique in this line? Have you been there before in the question that span has been around - will this tag again appear in a string or not? Regular expressions are not magic, they also need clear conditions. - Stanislav Belichenko

1 answer 1

First, we replace the commas with dots, if any, in order to realize the support of fractional numbers, and then we clean out everything except the dots and numbers, and convert them to float.

 $ss = str_replace(",",".", "<span>от 5 276 руб.</span>"); echo (float) preg_replace ("#[^.0-9]#", "", $ss); 
  • and what if the line is big? which one has this span? and can other numbers get? - user236477
  • four
    If the string is large or other numbers can be caught in it, then obviously it is worth pointing it in the context of the problem. - Stanislav Belichenko
  • one
    @ user236477, please show the full example of the string (s) for that matter, from which you need to remove the numbers, but it is not clear, you first say one thing. then another, then the third ... You already decide what line (s) you have and add to the question, and then to solve this problem, there are a lot of options. - And