View Full Version : preg_match help wanted
SoloX
12-05-2009, 03:43 AM
Hi all:
I have strings like
filename.jpg
I need to use preg_match or other PHP technique to get the two variables (34, filename.jpg) out. Can someone please help?
Thanks
Well I'm not a regex expert or anything but I thought I'd give it a shot:
$pat = '@\(.+)\[/media\]@';
$str = '[media=34]filename.jpg';
preg_match($pat, $str, $matches);
print_r($matches);
output is:
Array
(
[0] => filename.jpg
[1] => 34
[2] => filename.jpg
)
SoloX
12-05-2009, 04:58 AM
You are a confirmed genius, my friend. It works like a charm.
Thank you very much.
--------------- Added 1260000667 at 1260000667 ---------------
:D I am greedy and back for one more and hopefuly last Q for this project.
My strings can be
filename.jpg
or
http://www.d.com/filename.jpg
From the first example, I got $match[2] as either filename.jpg or httP://www.d.com/filename. Lets say I put that in another variable $var. How would I check if $var starts with http or not.
Hmm...maybe:
if (strncasecmp("http:", $var, 5) == 0) {
// This is a url
}
One thing I thought of that posting last night, it doesn't handle white space very well, it's possible to write a pattern that would ignore any white space on either end of the file name. (Or I guess you could also use trim() on the result if it were an issue).
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.