如何在PHP中的URL之间追加一个字符串

I want to add a string between a url string using PHP.

$link = 'http://localhost/wordpress/mypage';
$string = 'nl/';

I want the new link to be like this:

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

Here is one-of-the way to achieve it, using substr_replace():

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

echo substr_replace($someString, $string, strpos($someString, 'mypage'), 0);

Output:

http://localhost/wordpress/nl/mypage

Another method using str_replace():

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

echo str_replace('wordpress/', 'wordpress/nl/', $someString);

this is an easiest way you can do.

$string = 'nl/';
$link = 'http://localhost/wordpress/'.$string.'mypage';

You can set the $string dynamic or static as you wanted to.

echo $link; Result : http://localhost/wordpress/nl/mypage