Jump to content

[SOLVED] Need help on a regex


jeet_0077

Recommended Posts

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

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.