jeet_0077 Posted May 18, 2009 Share Posted May 18, 2009 Hi, I have a content stored in a variable as $var = 'some string some string some string some string some string /videos/videoname.mp4 some string some string some string some string some string some string ' ; Can some one help me to get the video name from the above string. Thanks a lot in advance ! Link to comment https://forums.phpfreaks.com/topic/158657-solved-need-help-on-a-regex/ Share on other sites More sharing options...
Adam Posted May 18, 2009 Share Posted May 18, 2009 preg_match_all('/[^\s]+\.mp4/', $var, $matches); This should work... It assumes that there are no spaces in the file path / name. Link to comment https://forums.phpfreaks.com/topic/158657-solved-need-help-on-a-regex/#findComment-836743 Share on other sites More sharing options...
nrg_alpha Posted May 19, 2009 Share Posted May 19, 2009 preg_match_all('/[^\s]+\.mp4/', $var, $matches); This should work... It assumes that there are no spaces in the file path / name. That would work for the whole path (assuming there is a space before the path)... If the goal is to get simply 'videoname.mp4', one could use a more specific character class (as [^\s]+ encompasses quite a few possible characters). Also, for a single search, you can simply use preg_match instead of preg_match_all. Assuming that the goal is indeed 'videoname.mp4', one way could be: preg_match('#[a-z]+\.mp4#i', $var, $match); echo $match[0]; If there is a need for more characters, perhaps replacing [a-z]+ with [\w-]+ (to cover a-zA-Z0-9_- if need be...depends on what bases you need covered in filenames I suppose). Link to comment https://forums.phpfreaks.com/topic/158657-solved-need-help-on-a-regex/#findComment-836873 Share on other sites More sharing options...
jeet_0077 Posted May 19, 2009 Author Share Posted May 19, 2009 Thank you guys thanks a lot. It really helped me. I really need to learn the regex its really cool. Link to comment https://forums.phpfreaks.com/topic/158657-solved-need-help-on-a-regex/#findComment-837283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.