phpregex

How can I extract css file using Php regular expression


I have an html file where I want to extract the name of the CSS file.
string:

{% INCLUDECSS '@cms_blog/blog.css?hash=8434bb8f' %}
preg_match_all("/{% INCLUDECSS '@(?<basename>(.*?))(\?hash=.*?)' %}/m", $string, $match);

output

array(5) {
  [0] => array(1) {
    [0] => string(51) "{% INCLUDECSS '@cms_blog/blog.css?hash=8434bb8f' %}"
  }
  ["basename"] => array(1) {
    [0] => string(17) "cms_blog/blog.css"
  }
  [1] => array(1) {
    [0] => string(17) "cms_blog/blog.css"
  }
  [2] => array(1) {
    [0] => string(17) "cms_blog/blog.css"
  }
  [3] => array(1) {
    [0] => string(14) "?hash=8434bb8f"
  }
}

How do I get the file name with the 'array basename' blog.css?

  ["basename"] => array(1) {
    [0] => string(17) "blog.css"
  }

Solution

  • Skip over everything until the last / before the basename capture group.

    preg_match_all("#{% INCLUDECSS '@.*?/(?<basename>[^/]*)(\?hash=.*?)' %}#", $string, $match);
    

    I changed the delimiters to # so I won't need to escape the literal / in the pattern.

    And I removed the m modifier, which is redundant when the pattern doesn't contain ^ or $.

    There's no need for parentheses after ?<basename>, it's capturing the same thing as the basename named group.

    I changed .*? in the basename group to [^/]* so it will match the filename only. This allows me to use .*? before the / to match non-greedily up to this, so it won't cross multiple of these groups.