VK27
VK27

Reputation: 525

str_replace. replacing numbers from a string

HI all I have this code here

$pathinfo = pathinfo($fullpath);
$tags = $shortpath;
$tags = str_replace("/", " ", $tags);
$tags = str_replace("__", " ", $tags);
$tags = str_replace(".png", "", $tags);
$tags = str_replace(".jpg", "", $tags);
$tags = str_replace(".jpeg", "", $tags);
$tags = str_replace(".gif", "", $tags);

Everything works fine with the above but I also need it to replace some numbers at the start of the files I am adding

example of a file would be

247991 - my_small_house.jpg

its the numbers before the "-" I need gone Can this be done?

Thanks

Upvotes: 1

Views: 3054

Answers (3)

Dippi
Dippi

Reputation: 397

Is the number you have to delete made of a fixed number of digits? If so, you could just do:

$tags = substr($tags, 9);

Else, if you're sure that every number ends with " - ", you could do:

$tags = substr($tags, strrpos($tags," - ") + 3);

Upvotes: 1

Damien Pirsy
Damien Pirsy

Reputation: 25435

You can use a regex with preg_replace() or preg_split(), but I think explode() would be better:

$chunks = explode('-',$shortpath);  // you just keep the part after the dash
$tags = str_replace(array('/','__'),' ', $chunks[1]);
$tags = str_replace(array('.png','.jpg','.jpeg','.gif'),'',$tags);
/* used array to avoid code repetition */

Upvotes: 2

Velcro
Velcro

Reputation: 556

Try this:

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

Upvotes: 0

Related Questions