For example, the value of the string $a = "абв351abc" , is it possible only with the help of php to pull a numeric value from this string?

  • And what do you mean by "means php". preg_math('/(\d+)/','абв351abc',$match); print $match[1] preg_math('/(\d+)/','абв351abc',$match); print $match[1] fits the definition of "php tool"? - Mike
  • And with a variable instead of a string, it will be preg_math ('/ (\ d +) /', $ name, $ match); print $ match [1]? - MaxPolsky
  • It will of course. php doesn't care if the string is somewhere or its variable contains - Mike
  • Thanks, I will understand! - MaxPolsky
  • one
    Yes, you can, pull it out. ) - Nick Volynkin

3 answers 3

There are filters for this:

 echo(filter_var('asdasd0755asdasdasd', FILTER_SANITIZE_NUMBER_INT));//0755 echo(filter_var('a13sdasd07.55asd', FILTER_SANITIZE_NUMBER_FLOAT , FILTER_FLAG_ALLOW_FRACTION)); //1307.55 

    The answer to the question - You can .

    Extended answer to the question (although questions need to be able to ask). You can pull a number from a string, for example, with a regular expression.

     $a = "абв351abc123"; // входная строка preg_match_all("/\d+/", $a, $match); // ищем все вхождения чисел от 1 цифры и далее if(isset($match[0])) { // проверяем, если ли совпадения foreach($match[0] as $number) { var_dump((int) $number); // приводим в числовому типу } } 

      If the number is one, you can simply delete the letters:

       $num=(int)preg_replace("/[A-zА-я]/","","абВ351abC"); print ("<br>num=$num"); 

      Result:

       num = 351
      

      If a number can have a sign and a decimal point, then:

       preg_match_all("/[+-]*[0-9]+[.]*[0-9]*/", "абВ-351.5abC", $match); $num = $match[0][0]+0; printf ("<br>num = $num"); 

      Result:

       num = -351.5
      
      • 26 letters in different registers against 10 digits. And if also punctuation marks and other symbols will get? So is not it easier to just numbers and pull out? - Vitalts
      • @Vitalts Numbers may contain a decimal point, signs and separators of groups of numbers. Although with the designation of the degree of e questions will arise at all. - Yuri Negometyanov
      • @Vitalts Checking letters in a row is a couple of conditions. By the way, in my first version, several characters fudalas along with the letters (there is a gap between Z and a). But if you need not only int, then the second option is more accurate. - Yuri Negometyanov