0

假设我有这个网址:

http://example.com/image-title/987654/

我想将“下载”插入“图像标题”和“987654”之间的部分,所以它看起来像:

http://example.com/image-title/download/987654/

帮助将不胜感激!谢谢你。

4

3 回答 3

3

假设您的 URI 始终是相同(或至少是可预测的)格式,您可以使用该explode函数将 URI 拆分为其每个部分,然后用于array_splice将元素插入该数组,最后用于implode将它们重新组合在一起成一个字符串。

$length请注意,您可以通过将参数指定为零来将元素插入到数组中。例如:

$myArray = array("the", "quick", "fox");
array_splice($myArray, 2, 0, "brown");
// $myArray now equals array("the", "quick", "brown", "fox");
于 2012-06-13T05:28:18.560 回答
0

格式不是很好,但我认为这是你需要的

$mystr= 'download';
$str = 'http://example.com/image-title/987654/';
$newstr = explode( "http://example.com/image-title",$str);
$constring =  $mystr.$newstr[1];


$adding = 'http://example.com/image-title/';
echo $adding.$constring;  // output-- http://example.com/image-title/download/987654/
于 2012-06-13T05:42:12.423 回答
0

在 PHP 中有很多方法可以做到这一点:

  • 使用 explode()、array_merge、implode() 进行拆分和重构
  • 使用子字符串()
  • 使用正则表达式
  • 使用 str_replace

假设所有 url 都符合相同的结构(image-title/[image_id]),我建议使用 str_replace,如下所示:

$url = str_replace('image-title', 'image-title/download', $url);

但是,如果图像标题是动态的(图像的实际标题),我建议像这样拆分和重建:

$urlParts = explode('/', $url);
$urlParts = array_merge(array_slice($urlParts, 0, 3), (array)'download', array_slice($urlParts, 3));
$url = implode('/', $urlParts);
于 2012-06-13T05:48:52.043 回答