如何在PHP中匹配和替换图像URL?

时间:2019-04-13 04:06:27

标签: php

我有如下网址图片:

https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.jpg

我想用URL替换大写字母,即_KOVW用PHP替换为_kovw

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

我敢肯定有解决问题的简便方法,但这只是一种方法。此方法将网址图片分为三组,其中第二组是目标大写字母(例如KOVW)。然后,将第二组小写并重建这些组。

脚本

// input image goes here
$capital_image = 'https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.jpg';
// divides original image to three groups 
preg_match_all('/(.+?)([A-Z]+)\.(jpg|jpeg|JPG|png|PNG|gif|GIF)/s', $capital_image, $matches);
// glues the pieces of image and lowercases the target part
$lowercase_image = $matches[1][0] . strtolower($matches[2][0]) . '.' . $matches[3][0];
// print the lowercase image
var_dump($lowercase_image);

输入图像

https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.jpg
https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.jpeg
https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.JPG
https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.png
https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.PNG
https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.gif
https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.GIF

输出匹配项

array(4) {
  [0]=>
  array(1) {
    [0]=>
    string(110) "https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.jpg"
  }
  [1]=>
  array(1) {
    [0]=>
    string(102) "https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_"
  }
  [2]=>
  array(1) {
    [0]=>
    string(4) "KOVW"
  }
  [3]=>
  array(1) {
    [0]=>
    string(3) "jpg"
  }
}

RegEx

您可以在this link中查看或修改其RegEx。

答案 1 :(得分:2)

您可以简单地使用str_replace

$url = 'https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.jpg';
$new_url = str_replace( '_KOVW', '_kovw', $url );
echo $new_url;

如果您的所有URL都存在相同类型的问题,则可以创建类似以下的函数:

function fixFileName( $url ) {

    $old_name = basename( $url ); // extract filename from url
    $new_name = strtolower( $old_name ); // lower case filename
    return str_replace( $old_name, $new_name, $url ); // replace old filename with new filename in url and return
}

// how to call function
$url = 'https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.jpg';
echo fixFileName( $url );

答案 2 :(得分:0)

您可以使用preg_replace_callback匹配url的开头,然后匹配非空格字符1次以上并下划线,然后忘记使用\K匹配的内容。

然后匹配1+次大写字符,后跟一个正向先行以断言右边是一个点,再匹配1+次一个字字符(或.jpg从字面上进行匹配)

https?://\S+_\K[A-Z]+(?=\.\w+$)

Demo on regex101 | Php demo

例如:

$str = "https://media.ex-cdn.com/EXP/media.khoeplus24h.vn/files/f1/Uploaded/lenguyet/2014_11_30/nhom1kienthuc_KOVW.jpg";
$str = preg_replace_callback("~https?://\S+_\K[A-Z]+(?=\.\w+$)~", function ($m){return strtolower($m[0]);} , $str);
echo $str;