提问人:Mehul Kumar 提问时间:2/2/2023 最后编辑:Mehul Kumar 更新时间:2/10/2023 访问量:85
如何在PHP中从字符串位置排除图像扩展名
How to exclude image extension from string place in PHP
问:
我想替换所有字符串值,以排除图像扩展名等。example.com
sample.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
答:
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.com
example.com
sample.com
PHP Array
$oldomain = ['example.com','www.'];
$newdomain= ['sample.com', '']
0赞
Mehul Kumar
2/3/2023
我正在尝试合并你和你的这个答案,结果是(错误):3v4l.org/qpOeUthis answer
https://stackoverflow.com/a/75311164/20776947
0赞
Mehul Kumar
2/3/2023
是的,这符合预期。所以,我可以添加多个扩展名,如,等。 再次感谢。css
js
1赞
RiggsFolly
2/3/2023
我想是的,但试一试
1赞
Rajeev
2/10/2023
#2
- 用
PATHINFO_EXTENSION
&str_replace
- 首先比
explode
implode
$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;
评论