如何在PHP中从字符串位置排除图像扩展名

How to exclude image extension from string place in PHP

提问人:Mehul Kumar 提问时间:2/2/2023 最后编辑:Mehul Kumar 更新时间:2/10/2023 访问量:85

问:

我想替换所有字符串值,以排除图像扩展名等。example.comsample.com.png.jpeg.jpg

$body = ' www.example.com, example.com, example.com/hello.html, example.com/logo.png, example.com/yourbaby.php , example.com/newimage.jpg, www.example.com/newimage.jpg';

//values    
$oldomain = ['example.com','www.']; 
$newdomain= ['sample.com', ''];
$excludeextension = [".png", ".jpg", ".jpeg"];

//Replace    
$body = str_replace($olddomain, $newdomain, $body);
$body = str_replace($excludeextension, '', $body);

//output    
 echo $body; 

我正在寻找的输出:

sample.com, sample.com, sample.com/hello.html, example.com/logo.png, sample.com/yourbaby.php , example.com/newimage.jpg, www.example.com/newimage.jpg

期望

https://example.com -> https://sample.com

https://www.example.com -> https://sample.com

https://subdomain.example.com -> https://subdomain.sample.com

https://www.example.com/image.png -> https://www.example.com/image.png

https://example.com/image.png -> https://example.com/image.png
php 正则表达式 替换 preg-replace str-replace

评论

2赞 ADyson 2/2/2023
你试过什么吗?你被困在哪里了?如有必要,我们很乐意提供帮助,但这不是免费的外包服务。请阅读如何提问
0赞 RiggsFolly 2/3/2023
良好的代码缩进和布局将帮助我们阅读代码,更重要的是,它将帮助您调试代码 为了您自己的利益,快速浏览一下编码标准。您可能会被要求在几周/几个月内修改此代码,您最终会感谢我。
0赞 Mehul Kumar 2/3/2023
@RiggsFolly很抱歉我在写问题时误解了。我已经更新了所有内容。
0赞 RiggsFolly 2/3/2023
@MehulKumar 不用了,我想误会都是我的:)

答:

2赞 RiggsFolly 2/3/2023 #1

你可以用一些爆炸和内爆来做到这一点。str_replace()

$body = ' www.example.com, example.com, example.com/hello.html, example.com/logo.png, example.com/yourbaby.php, example.com/newimage.jpg, www.example.com/newimage.jpg';

$oldomain = ['example.com','www.']; 
$newdomain= ['sample.com', ''];
$excludeextension = ["png", "jpg", "jpeg"];

$doms = explode(',', $body);
foreach ($doms as &$dom) {
    // get extension
    $pi = pathinfo($dom);
    if ( ! in_array( $pi['extension'], $excludeextension) ){
        $dom = str_replace($oldomain, $newdomain, $dom);
    }

}
$NewStr = implode($doms);
echo $NewStr;

结果

sample.com sample.com sample.com/hello.html example.com/logo.png sample.com/yourbaby.php example.com/newimage.jpg www.example.com/newimage.jpg

评论

0赞 Mehul Kumar 2/3/2023
我已经更新了我的问题。正如您昨天在这里提到的,我正在使用数组:stackoverflow.com/a/75311164/20776947 。我已经用你昨天的答案尝试了这个答案,但它给出了一个错误。PHP Parse error: syntax error, unexpected '$excludeextension' (T_VARIABLE)
0赞 Mehul Kumar 2/3/2023
您的代码中没有错误。但就像昨天我说或应该改成.所以你给了我一个解决方案来使用.因此,当我使用数组 like 和 时,我收到错误。我也更新了我的问题www.example.comexample.comsample.comPHP Array$oldomain = ['example.com','www.'];$newdomain= ['sample.com', '']
0赞 Mehul Kumar 2/3/2023
我正在尝试合并你和你的这个答案,结果是(错误):3v4l.org/qpOeUthis answerhttps://stackoverflow.com/a/75311164/20776947
0赞 Mehul Kumar 2/3/2023
是的,这符合预期。所以,我可以添加多个扩展名,如,等。 再次感谢。cssjs
1赞 RiggsFolly 2/3/2023
我想是的,但试一试
1赞 Rajeev 2/10/2023 #2
  1. PATHINFO_EXTENSION & str_replace
  2. 首先比explodeimplode

$body = 'example.com, www.example.com, https://www.example.com, https://example.com, subdomain.example.com, example.com/logo.png, www.example.com/logo.png, example.com/hello/logo.png, https://example.com/logo.png, subdomain.example.com/logo.png"';

//Defined Values 
$oldomain = ["example.com", "www.example.com"];
$newdomain = "sample.com";
$excludeextension = ["png", "jpg", "jpeg", "css"];

// Split the string into an array of URLs
$urls_body_extension = explode(", ", $body);

// Loop through each URL and replace it if necessary
foreach ($urls_body_extension as &$url_body_extension) {
    $extension_body_extension = pathinfo($url_body_extension, PATHINFO_EXTENSION);
    if (!in_array($extension_body_extension, $excludeextension)) {
        foreach ($oldomain as $domain_body_extension) {
            if (strpos($url_body_extension, $domain_body_extension) !== false) {
                $url_body_extension = str_replace($domain_body_extension, $newdomain, $url_body_extension);
                break;
            }
        }
    }
}

// Join the modified URLs back into a string
$body = implode(", ", $urls_body_extension);

echo $body;