从图片网址中删除字符[重复](Remove character from image url [duplicate])

这个问题在这里已有答案:

如何删除查询字符串并只获取网址? 12个答案

我是PHP和WordPress的新手,我需要使用WordPress函数download_URL()从离岸网站导入图像。

我的问题是,我的网址看起来像这样:

HTTP://www.test/img/FMR.jpg

但我也有这样的URL:

HTTP://www.test/img/FMR.jpg Qq_0y12h

我可以删除.extension之后的所有内容吗?

This question already has an answer here:

How to remove the querystring and get only the url? 14 answers

I'm new to PHP and WordPress, and i need to import image from a offshore website with the WordPress function download_URL().

My problem is, i have URL that look like that :

http://www.test/img/FMR.jpg

But i have also URL that look like that :

http://www.test/img/FMR.jpg?Qq_0y12h

And can i have remove everything that is after the .extension if there something?

最满意答案

你可以使用preg_replace,我为你做了这个小例子。

<?php $url = "http://www.test/img/FMR.jpg?Qq_0y12h"; echo "url = $url\n\n"; $urlFormatted = preg_replace("/\?.*$/", "", $url); echo "urlFormatted = $urlFormatted\n"; ?>

you can use preg_replace, I did this little example for you.

<?php $url = "http://www.test/img/FMR.jpg?Qq_0y12h"; echo "url = $url\n\n"; $urlFormatted = preg_replace("/\?.*$/", "", $url); echo "urlFormatted = $urlFormatted\n"; ?>从图片网址中删除字符[重复](Remove character from image url [duplicate])

这个问题在这里已有答案:

如何删除查询字符串并只获取网址? 12个答案

我是PHP和WordPress的新手,我需要使用WordPress函数download_URL()从离岸网站导入图像。

我的问题是,我的网址看起来像这样:

HTTP://www.test/img/FMR.jpg

但我也有这样的URL:

HTTP://www.test/img/FMR.jpg Qq_0y12h

我可以删除.extension之后的所有内容吗?

This question already has an answer here:

How to remove the querystring and get only the url? 14 answers

I'm new to PHP and WordPress, and i need to import image from a offshore website with the WordPress function download_URL().

My problem is, i have URL that look like that :

http://www.test/img/FMR.jpg

But i have also URL that look like that :

http://www.test/img/FMR.jpg?Qq_0y12h

And can i have remove everything that is after the .extension if there something?

最满意答案

你可以使用preg_replace,我为你做了这个小例子。

<?php $url = "http://www.test/img/FMR.jpg?Qq_0y12h"; echo "url = $url\n\n"; $urlFormatted = preg_replace("/\?.*$/", "", $url); echo "urlFormatted = $urlFormatted\n"; ?>

you can use preg_replace, I did this little example for you.

<?php $url = "http://www.test/img/FMR.jpg?Qq_0y12h"; echo "url = $url\n\n"; $urlFormatted = preg_replace("/\?.*$/", "", $url); echo "urlFormatted = $urlFormatted\n"; ?>