[SOLVED] how can i extract a substring maybe by using a regu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

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

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

$string = "0421045_01.txt";

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

echo $components[0];
Mark
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

sorry the correctfull string is :
files/0421045_01.txt
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Just modify Mark's slightly then:

Code: Select all

$string = "files/0421045_01.txt";
$components = explode(".", basename($string));
echo $components[0];
AimsB
Forum Newbie
Posts: 12
Joined: Thu Jul 08, 2004 9:39 am
Location: UK

Post by AimsB »

basename, never heard of that before - what a useful function! I was just always splitting on the "/"!
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

yep i agree with u
Post Reply