17

I have a string like this:

" 23 PM"

I would like to remove 23 so I'm left with PM or (with space truncated) just PM.

Any suggestions?

Needs to be in PHP

1
  • 3
    I would carefully read through each answer and choose the best solution for your needs. There are many varying answers and some have been unnecessarily downvoted as a result. Commented Jul 29, 2010 at 14:13

9 Answers 9

61
echo trim(str_replace(range(0,9),'',' 23 PM'));
Sign up to request clarification or add additional context in comments.

Comments

16

Can do with ltrim

ltrim(' 23 PM', ' 0123456789');

This would remove any number and spaces from the left side of the string. If you need it for both sides, you can use trim. If you need it for just the right side, you can use rtrim.

Comments

13
preg_replace("/[0-9]/", "", $string);

3 Comments

Even this would match all digits, I guess it would be faster with the asterisk after the paranthesis, like in my answer.
It's not going to make much difference in speed using an astrisk vs not... And even at that, you're talking about a time value with 4 zeros after the decimal point anyway (0.00001 seconds or so), so unless you're doing it in a loop (a large loop), there's no point in trying to optimize a statement like this...
Space removing wasn't the main point of view. But it's really easy to do that too. Just put space into brackets: [0-9 ]
5

Can also use str_replace, which is often the faster alternative to RegEx.

str_replace(array(1,2,3,4,5,6,7,8,9,0,' '),'', ' 23 PM');
// or
str_replace(str_split(' 0123456789'), '', ' 23 PM');

which would replace any number 0-9 and the space from the string, regardless of position.

Comments

5

If you just want the last two characters of the string, use substr with a negative start:

$pm = substr("  23 PM", -2); // -> "PM"

1 Comment

upvoting this to compensate unjustified downvote and also because the OP is not clear about the strings he expects and Andy explicitly stated what the code above does.
3
$str = preg_replace("/^[0-9 ]+/", "", $str);

3 Comments

This would say, that the numbers have to be on the left side, and that there must be at least one number. This fits for the example he provided, but in general this solution wouldn't work. What about strings like "AM 23423NNfB"? You couldn't remove the numbers with your regex.
It answers the request of the op, he didn't say anything about what there is after PM.
He said. "I've got a string LIKE...." And the headline of the topic says generally: Remove spaces from string. So I understand "ANY String".
3

Regex

preg_replace('#[0-9 ]*#', '', $string);

Comments

2

You can also use the following:

preg_replace('/\d/', '',' 23 PM' );

Comments

1

trim() will allow a range of characters in the "character mask" parameter by using dot syntax.

Code: (Demo)

//              v--------literal spaces
trim(" 23 PM", ' 0..9')
//               ^^^^----all digits
// output: PM

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.