tags:

views:

46

answers:

5

How would I go about removing numbers and a space from the start of a string?

For example, from '13 Adam Court, Cannock' remove '13 '

Thanks!

(Apologies for near duplicate, I realised I need to use PHP to achieve this)

+1  A: 

Try this one :

^\d+ (.*)$

Like this :

preg_replace ("^\d+ (.*)$", "$1" , $string);

Resources :

On the same topic :

Colin Hebert
+1  A: 

Use the same regex I gave in my JavaScript answer, but apply it using preg_replace():

preg_replace('/^\d+\s+/', '', $str);
BoltClock
Hmm it turns out the variable I am running the regex on also spits out a load of HTML, in this instance it would be still be safe to remove all numbers, so would that just be the 'd'? Thanks alot for your response!
Probocop
@Probocop: if you want to remove numbers only from the beginning, use `/^\d+/`; if you want to remove all numbers, drop the `^`.
BoltClock
That's fantastic, thank you very much! (I really need to learn regular expressions when I get time)
Probocop
Interesting that you gave *both* me and NullUserException the +15 rep by accepting our different answers on each question. Thanks on our behalf :)
BoltClock
+1  A: 

I'd use

/^\d+\s+/

It looks for a number of any size in the beginning of a string ^\d+

Then looks for a patch of whitespace after it \s+

When you use a backslash before certain letters it represents something...

\d represents a digit 0,1,2,3,4,5,6,7,8,9.

\s represents a space .

Add a plus sign (+) to the end and you can have...

\d+ a series of digits (number)

\s+ multiple spaces (typos etc.)

Mark
Thanks for the explanation!
Probocop
+1  A: 

The same regex I gave you on your other question still applies. You just have to use preg_replace() instead.

Search for /^[\s\d]+/ and replace with the empty string. Eg:

$str = preg_replace(/^[\s\d]+/, '', $str);

This will remove digits and spaces in any order from the beginning of the string. For something that removes only a number followed by spaces, see BoltClock's answer.

NullUserException
+1  A: 

Because everyone else is going the \d+\s route I'll give you the brain-dead answer

$str = preg_replace("#([0-9]+ )#","",$str);

Word to the wise, don't use / as your delimiter in regex, you will experience the dreaded leaning-toothpick-problem when trying to do file paths or something like http://

:)

BjornS