Help check the link for the presence of a file extension at the end, that is, there is a dot and file types behind it (.html; .htm; .png; .jpeg, etc.).

For example, a reference like component/tv/samsung/21.html - must match a specific regular expression.

I tried to make up and here's what I got:

 if (preg_match('/^component\/([a-zA-Z-_0-9]*)\/([a-zA-Z-_0-9]*)\/(.+)$/i', 'component/tv/samsung/21.html', $matches)) { print_r($matches); } 

But I would like to replace (. +) With a more stringent condition. Help, who knows.

    2 answers 2

    try this

     /^component\/([a-zA-Z-_0-9]*)\/([a-zA-Z-_0-9]*)\/(.+)(html|htm|png|jpeg)$/i 

    or

     /^component\/([a-zA-Z-_0-9]*)\/([a-zA-Z-_0-9]*)\/(.+)\.(html|htm|png|jpeg)$/i 
    • one
      component/tv/samsung/21/?type=.jpeg ? .. - Alekcvp

    As an option : ^component\/([\w-%~]+)\/([\w-%~]+)\/([\w-%~]*\.\w*)$ if you know exactly what you have two folders in the path and after them the name of the file. If the exact number of folders is not known, the request will become much more complicated. [\ w-% ~] captures a_z, A_Z, 0_9, _ and optionally '-', '%', '~' (space in the link is% 20, for example). Perhaps offhand, I missed any other valid characters.


    Instead of the latter, \w* you can, of course, hard-specify file extensions, but only if you are sure that you know them all and that they will not change: (htm|html|php|jpg|jpeg|...) .

    • Thanks for the answer! Tell me, please, if the number of folders is not known. For example, for all links on the site. - Pavel
    • Will it be correct: /^(.+\.\w*)$/i ? - Pavel
    • You just need to check the link for the presence of a point and the extension or extract some parts from it? .. If you just check, then probably it would be better like this: /^([^\?#]+\.\w*)(?:[\?#].*)?$/i , because The link may contain parameters (? param = value) and bookmarks (#bookmark). As a result, such RegEx will capture only the link (without parameters) if it contains the file name. And this regex does not check the link to validity, since believes that it is initially correct. You can check it here: regex101.com/r/rC9yU7/1 - Alekcvp
    • Thanks a lot! - Pavel