Hi, I got a string like this: some text Some text 2.9 3.00 1.2 300.20 4444.666 100000.3333441 I want to remove the decimal numbers from this string without using explode() and then checking each value fwith is_numeric() I think it'll be better if I can use preg_replace. Please note that the string can also be like this: some text 400 Some text 2.9 3.00 1.2 300.20 4444.666 100000.3333441 I don't want to remove the number between the text, just the ones at the end of the text. What will be the preg_replace pattern? Thanks
What would the result be in both cases? Do you just want to remove decimals and leave integers alone?
What exactly would you be replacing it WITH? The question doesn't make a lot of sense, at least not without some idea what you want for output and what you want to do with that output.
Never mind guys! I did this, but without using preg. deathshadow, I just wanted to remove those, not replace. So I would have used a empty "" in the preg_replace... I like your signature. So true!!! I deleted the script which did the job, but here's how it went: The string was exploded using white space. $array = explode(' ',$str); Each element was checked if it was numeric and had a '.' sign using strpos If it did, the array element was unset. Then finally an implode. $final= implode(' ',$array); Thanks
preg_replace was a good idea. You need to leave a text only, right? So here is javascript example, but regex will be the same for PHP: 'some text 400.33 Some text 2.9 3.00 1.2 300.20 4444.666 100000.3333441'.replace(/[\.\s\d]+$/, '') Code (markup): This regex means to remove all ([]+) dots (\.), spaces (\s) and numbers (\s) from the end of the string ($)
Wrong, If I interpreted the question right (hard with the ranguage rarrier) the 400 in the middle needed to stay. Basically they want to only strip them from the end... and I believe only if is_float.
Hi JEET. $strIn = 'some text 400 Some text 2.9 3.00 1.2 300.20 4444.666 100000.3333441'; $strOut = preg_replace('/(\d+)\.\d+/', '$1', $strIn); echo $strOut; PHP: Output: some text 400 Some text 2 3 1 300 4444 100000 Code (markup): Is this what you need?