Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.