简体   繁体   中英

php regex exclude string in url

I have a regular expression

\(\'?.*(\/public\/images.*[^\'"])\'?\)

It matches ulrs in my CSS file. Here is examples:

  1. background: url( /public/images/new_layout/bg-nav-active.png ) no-repeat 100% 100%;
  2. background: url( /public/images/new_layout/bg-nav-active.png?v=23423h423lj4h23l4jk23hl4jkh4h2kljh ) no-repeat 0 -

But, how to exclude parameter v from URL ?v=23423h423lj4h23l4jk23hl4jkh4h2kljh

Thank You for help.

Something like this should suffice;

\(\'?.*(\/public\/images[^\'"\?\)]+)

https://regex101.com/r/eZ7iO9/1

This will match the string "/public/images/" and everything that isn't in the character '"?)

This regex will match just the URLs, without any parentheses or ?v s due to the a lookbehind and a lookahead:

 (?<!\()['"]?.*(\/public\/images[^'"\?\)]+)

The non-greedy .+? makes it possible not to match ?v s.

See example here .

The output of your 2 input strings is:

/public/images/new_layout/bg-nav-active.png
/public/images/new_layout/bg-nav-active.png
str_replace('v=', '', $string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM