Page 1 of 1

how can i extract a substring maybe by using a regular expre

Posted: Thu Jul 08, 2004 4:08 am
by pelegk2
how can i extract a substring maybe by using a regular expresion?
for example i have this file name :
0421045_01.txt
and i want to extract only :
0421045_01
how can ido this?
i know always that what i want to etract start with a number and alwats end before the dot (.)
what is the best way to do ythis?
thnaks in advance
peleg

Posted: Thu Jul 08, 2004 4:32 am
by JayBird

Code: Select all

$string = "0421045_01.txt";

$components = explode(".", $string);

echo $components[0];
Mark

Posted: Thu Jul 08, 2004 4:40 am
by pelegk2
sorry the correctfull string is :
files/0421045_01.txt

Posted: Thu Jul 08, 2004 4:42 am
by markl999
Just modify Mark's slightly then:

Code: Select all

$string = "files/0421045_01.txt";
$components = explode(".", basename($string));
echo $components[0];

Posted: Thu Jul 08, 2004 9:39 am
by AimsB
basename, never heard of that before - what a useful function! I was just always splitting on the "/"!

Posted: Sun Jul 11, 2004 12:27 am
by pelegk2
yep i agree with u