Errol Pereira
Errol Pereira

Reputation: 11

How to append a string in between a url in PHP

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

Answers (2)

Zinc
Zinc

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

Milan Chheda
Milan Chheda

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

Related Questions