【问题标题】:How to append a string in between a url in PHP如何在PHP中的url之间附加一个字符串
【发布时间】:2018-05-09 15:44:42
【问题描述】:

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

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

我希望新链接是这样的:

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

【问题讨论】:

    标签: php string url append


    【解决方案1】:

    这是最简单的方法。

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

    您可以根据需要设置$string 动态或静态。

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

    【讨论】:

      【解决方案2】:

      这是实现它的一种方法,使用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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-15
        • 2019-12-04
        • 1970-01-01
        • 2018-03-19
        • 2011-05-25
        • 1970-01-01
        • 2011-08-07
        相关资源
        最近更新 更多