【问题标题】:Replace any string before "/", PHP替换“/”之前的任何字符串,PHP
【发布时间】:2010-02-17 08:45:19
【问题描述】:

我想替换“/”之前的任何字符串,无论字符串长度如何。

谢谢 让

【问题讨论】:

  • 你应该展示一个你想要的例子

标签: php


【解决方案1】:

一种方式,假设您要更改第一个“/”之前的字符串。

$str = "anystring/the_rest/blah";
$s = explode("/",$str);
$s[0]="new string";
print_r ( implode("/",$s) );

【讨论】:

  • 你知道,有趣的是,我尝试了这些东西,我挠头,没有我的球,然后堆积溢出。谢谢
【解决方案2】:
echo preg_replace('/^[^\/]+/', 'baz', 'foo/bar');

【讨论】:

    【解决方案3】:

    这样的方法是最有效的,尽管我仍然更喜欢 preg_replace() 技术

    $pos = strpos($input, '/');
    if ($pos >= 0) {
        $output = $replacement . substr($input, $pos);
    } else {
        $output = $input;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 2012-08-29
      • 1970-01-01
      • 2012-06-25
      • 2012-09-04
      • 1970-01-01
      相关资源
      最近更新 更多