【发布时间】:2018-05-09 15:44:42
【问题描述】:
我想使用 PHP 在 url 字符串之间添加一个字符串。
$link = 'http://localhost/wordpress/mypage';
$string = 'nl/';
我希望新链接是这样的:
$newlink = 'http://localhost/wordpress/nl/mypage';
【问题讨论】:
我想使用 PHP 在 url 字符串之间添加一个字符串。
$link = 'http://localhost/wordpress/mypage';
$string = 'nl/';
我希望新链接是这样的:
$newlink = 'http://localhost/wordpress/nl/mypage';
【问题讨论】:
这是最简单的方法。
$string = 'nl/';
$link = 'http://localhost/wordpress/'.$string.'mypage';
您可以根据需要设置$string 动态或静态。
echo $link; 结果:http://localhost/wordpress/nl/mypage
【讨论】:
这是实现它的一种方法,使用substr_replace():
$someString = 'http://localhost/wordpress/mypage';
$string = 'nl/';
echo substr_replace($someString, $string, strpos($someString, 'mypage'), 0);
输出:
http://localhost/wordpress/nl/mypage
使用str_replace()的另一种方法:
$someString = 'http://localhost/wordpress/mypage';
echo str_replace('wordpress/', 'wordpress/nl/', $someString);
【讨论】: