如何在PHP中的url之间添加字符串

如何在PHP中的url之间添加字符串,php,string,url,append,Php,String,Url,Append,我想使用PHP在url字符串之间添加一个字符串 $link = 'http://localhost/wordpress/mypage'; $string = 'nl/'; 我希望新链接如下所示: $newlink = 'http://localhost/wordpress/nl/mypage'; 下面是一种实现方法,使用substr\u replace(): 输出: http://localhost/wordpress/nl/mypage 使用str\u replace()的另一种方法:

我想使用PHP在url字符串之间添加一个字符串

$link = 'http://localhost/wordpress/mypage';
$string = 'nl/';
我希望新链接如下所示:

$newlink = 'http://localhost/wordpress/nl/mypage';

下面是一种实现方法,使用
substr\u replace()

输出:

http://localhost/wordpress/nl/mypage
使用
str\u replace()
的另一种方法:


这是一种最简单的方法

$string = 'nl/';
$link = 'http://localhost/wordpress/'.$string.'mypage';
您可以根据需要设置
$string
动态或静态

echo$link结果:
http://localhost/wordpress/nl/mypage

$someString = 'http://localhost/wordpress/mypage';

echo str_replace('wordpress/', 'wordpress/nl/', $someString);
$string = 'nl/';
$link = 'http://localhost/wordpress/'.$string.'mypage';