Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stripping out all characters from a string, leaving numbers

Tags:

php

numbers

Hay, i have a string like this:

v8gn5.8gnr4nggb58gng.g95h58g.n48fn49t.t8t8t57 

I want to strip out all the characters leaving just numbers (and .s)

Any ideas how to do this? Is there a function prebuilt?

thanks

like image 976
dotty Avatar asked Jan 22 '10 14:01

dotty


People also ask

How do I remove all characters from a string except numbers?

To remove all characters except numbers in javascript, call the replace() method, passing it a regular expression that matches all non-number characters and replace them with an empty string. The replace method returns a new string with some or all of the matches replaced.

How do you delete everything from a cell except a number?

Notes: (1) You can type the formula =EXTRACTNUMBERS(A2,TRUE) into selected cell directly, and then drag the Fill handle to the range as you need. (2) This EXTRACTNUMBERS function will also remove all kinds of characters except the numeric characters.

How do I remove a character from a number string?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.


2 Answers

$str = preg_replace('/[^0-9.]+/', '', $str); 

replace substrings that do not consist of digits or . with nothing.

like image 117
Don Avatar answered Oct 10 '22 13:10

Don


preg_replace('/[^0-9.]/', '', $string); 
like image 23
Ionuț G. Stan Avatar answered Oct 10 '22 14:10

Ionuț G. Stan