如何在没有http的情况下查找所有带有连字符的jpg?

时间:2018-10-17 20:44:30

标签: php regex

如果您有以下字符串:

$str = ';s:27:"2018/08/hello-there-003.jpg";s:5:"sizes";a:23:{s:9:"thumbnail";a:4:{s:4:"file";s:27:"hello-there-003-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:27:"hello-there-003-300x200.jpg"';

是否可以从中获取所有jpg并将其替换为其他图片?

匹配项应为:

hello-there-003-150x150.jpg
hello-there-003-300x200.jpg

并替换为:

replacement.jpg
replacement.jpg

因此,结果字符串为:

 $str = ';s:27:"2018/08/replacement.jpg";s:5:"sizes";a:23:{s:9:"thumbnail";a:4:{s:4:"file";s:27:"replacement.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:27:"hello-there-003-300x200.jpg"';

1 个答案:

答案 0 :(得分:0)

对于示例数据,您可以使用:

phpSuggestVariableName()

Regex demo

替换为

"\K[\w-]+(\.jpg)(?=")

说明

  • replacement$1匹配“
  • "忘记匹配的内容
  • \K匹配单词字符或连字符1次以上
  • [\w-]+在捕获组中匹配.jpg
  • (\.jpg)肯定在右边的内容是双引号

例如:

(?=")

Php demo