我想更改我网址的第一部分。
我有这个网址
www.website.com/download/long-file-name-123
我想更改"下载"部分到"下载"像这样。
www.website.com/downloads/long-file-name-123
问题是网址也可能如下所示:
www.website.com/download/files/file/long-file-name-123
所以我需要一些方法来始终只更改网址的第一部分,
答案 0 :(得分:1)
php > $url = 'www.website.com/download/long-file-name-123';
php > $newUrl = str_replace('www.website.com/download', 'www.website.com/downloads', $url);
php > var_dump($newUrl);
string(44) "www.website.com/downloads/long-file-name-123"
答案 1 :(得分:0)
for (let i = 0; i < 2; i++) {
// i is visible
}
// i is no longer visible as it's scope is only inside `for`
首次出现下载
<?php
echo str_replace("download","downloads","www.website.com/download/long-file-name-123");
?>
输出:
<?php
$urls = array("www.website.com/download/long-file-name-123","www.website.com/download/files/file/long-file-name-123");
$arrlength = count($urls);
for($x = 0; $x < $arrlength; $x++)
{
$pos = strpos($urls[$x], "download");
if ($pos !== false) {
$newstring = substr_replace($urls[$x], "downloads", $pos, strlen("download"));
echo ($newstring);
echo ("\n");
}
}
?>
现场演示: