Jump to content

preg_replace Keep Leading 0's

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
rsnider19

rsnider19

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
Say I have a string 001-234. I want to remove the hyphen, but keep the leading 0's. I have the following line of code:

$b = preg_replace("[\D]", "", $b);

Not only do I want to remove the hyphen, but as the code implies, remove everything but the digits.

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
$b = preg_replace("[(0-9)+]", "", $b); 

Something like this would probably work.

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
You are close. You need to delimit your regular expression.

$b = preg_replace("/\D/", "", $b);