problem writing a Pattern for preg_match_all

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
sina_saeedi82
Forum Newbie
Posts: 11
Joined: Sun Aug 23, 2009 5:13 am

problem writing a Pattern for preg_match_all

Post by sina_saeedi82 »

I want to use preg_match_all() function to finding some kind of spacial codes.
The codes are:

Code: Select all

[podcast]http://www.mydomain.com/radio/x.mp3[/podcast]
and
[podcast]http://www.mydomain.com/multimedia/x.flv[/podcast]
x is a file name and contains A-Z, a-z, 0-9, _, -

But I cant write the Pattern :(
Please help me :(
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: problem writing a Pattern for preg_match_all

Post by tr0gd0rr »

Start easy. First, find content between [podcast] tags. Remember to use a backslash to escape brackets and slashes:

Code: Select all

/\[podcast\](.*?)\[\/podcast\]/
From there, you could use `pathinfo` in php 5.2:

Code: Select all

print_r(pathinfo("http://www.mydomain.com/radio/x.mp3"));
Array
(
    [dirname] => http://www.mydomain.com/radio
    [basename] => x.mp3
    [extension] => mp3
    [filename] => x
)
If you would rather use a regex to do it in all one step, insert a pattern to match everything between the last slash and last dot. You can use the exact ranges you gave:

Code: Select all

\/([A-Za-z0-9_-]+)\.[A-Za-z0-9]+$
Post Reply