php数组替换


php array replacement

这是一个数组问题

如果我有这个$URI = 'http://localhost/id/artikel/info_artikel/1141/'我想使用$filterURL = explode('/', $URI);来拆分此url然后将特定值替换为如下所示:

        $filter = str_replace('id', 'en', $filterURL[3]);
        $sectionname_translated = str_replace($value, $key, $filterURL[4]);
        $filterfilename = explode('?', $filterURL[5]);
        $filename = $filterfilename[0];
        $newfilename = str_replace($filename, 'articles', $filename);

如何使用原点$filterURL重构回替换的数组值?因为数组的数量可能会有所不同,但只有这个数量的数组需要做替换。

提前谢谢。

我想你正在寻找这个:http://php.net/manual/en/function.implode.php

请记住,您有各种函数,如parse_url或parse_str,用于处理url。

例如:

$URI = 'http://localhost/something/index.php?test1=var1&test2=var2';
$parseUrl = parse_url($URI);
parse_str($parseUrl['query'], $parseUrl['query']);
array
  'scheme' => string 'http' (length=4)
  'host' => string 'localhost' (length=9)
  'path' => string '/something/index.php' (length=20)
  'query' => 
    array
     'test1' => string 'var1' (length=4)
     'test2' => string 'var2' (length=4)

然后你可以恢复过程:

$parseUrl['query'] = http_build_query($parseUrl['query']);
$parseUrl = http_build_url($parseUrl);

至于回复爆炸过程,它就像m_w所说的内爆。