Reputation:
So I have a file 6647_241_file1.pdf
and I need to get rid of the 6647_241_
part of the filename.
My original name comes from a row, but I created what I was doing below in a simple way.
<?php
$string=$row[file_name];
becomes:
$string="6647_241_file1.pdf";
$string1 = preg_replace("/*._/", "", $string);
echo $string1;
?>
I was just trying this but haven't been successful at it. The numbers change, so I wasn't sure of a universal way to remove them?
Upvotes: 1
Views: 133
Reputation: 2690
Try this regexp /^[\d_]*/
$string="6647_241_file1.pdf";
$string1 = preg_replace("/^[\d_]*/", "", $string);
echo $string1;
Should echo: file1.pdf
What does it do:
^
means look at the begining of the string[]
means group a couple of characters or characterclasses together as one\d
is the character class for digits which are numbers_
is what it is the character himself*
means the group []
can accour zero or more timesIn one sentence: Select all numbers and underscores from the beginning till something else comes.
Upvotes: 2
Reputation: 79014
There are regexes and explodes, etc. But here's one fun way:
$result = ltrim($string, '0123456789_');
Use trim()
if you want to trim both ends.
Upvotes: 1