Reputation: 11
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';
Upvotes: 0
Views: 116
Reputation: 385
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
Upvotes: 0
Reputation: 8249
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);
Upvotes: 1