Laravel Regex Match Url with JPG not working

Mavichow

I at http://www.regexr.com/ match a regex that needed for laravel 's regex validation. validate the url end with jpg/gif/png file.

/(http|https):\/\/(www\.)?[\w-_\.]+\.[a-zA-Z]+\/((([\w-_\/]+)\/)?[\w-_\.]+\.(png$|gif$|jpe?g$))/ig 'regex' But when i put my code, it throw me an preg_match(): No ending delimiter '/' found error.

here are my validation rule

$rules  = [
            'title'             => 'required|min:3',
            'description'       => 'required|min:3',
            'url'               => 'required|regex:/(http|https):\/\/(www\.)?[\w-_\.]+\.[a-zA-Z]+\/((([\w-_\/]+)\/)?[\w-_\.]+\.(png$|gif$|jpe?g$))/ig',
          ];
Robin

As mentionned here, Laravel | delimiter is conflicting with the regex alternation |.

The regex considered in

required|regex:/(http|https)...

is /(http, thus missing the ending delimiter.

Seems you can fix the issue by using (I took the liberty to clean a few things in your regex):

'url' => array('required', 'regex:/(https?):\/\/(www\.)?[\w.-]+\.[a-zA-Z]+\/((([\w\/-]+)\/)?[\w.-]+\.(png|gif|jpe?g)$)/ig')

If Laravel is okay with using different delimiters than / I'd also recommend using something else (~ for example) to lighten the forest of \/.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章