【问题标题】:How do I change the delimiter of a list?如何更改列表的分隔符?
【发布时间】:2011-04-13 17:40:06
【问题描述】:
$variable = 'one, two, three';

如何将单词之间的逗号替换为<br>

$variable 应该变成:

one<br>
two<br>
three

【问题讨论】:

    标签: php explode


    【解决方案1】:

    要么使用str_replace

    $variable = str_replace(", ", "<br>", $variable);
    

    或者,如果您想对介于两者之间的元素做其他事情,explode()implode()

    $variable_exploded = explode(", ", $variable);
    $variable_imploded = implode("<br>", $variable_exploded);
    

    【讨论】:

      【解决方案2】:
      $variable = str_replace(", ","<br>\n",$variable);
      

      应该做的伎俩。

      【讨论】:

        【解决方案3】:
        $variable = explode(', ',$variable);
        $variable = implode("<br/>\n",$variable);
        

        然后你就可以echo $variable

        【讨论】:

          【解决方案4】:

          你可以这样做:

          $variable = str_replace(', ',"<br>\n",$variable);
          

          【讨论】:

            【解决方案5】:
            $variable = preg_replace('/\s*,\s*/', "<br>\n", $variable);
            

            这会将您带入正则表达式领域,但这将处理逗号之间随机间距的情况,例如

            $variable = 'one,two, three';
            

            $variable = 'one , two, three';
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-07-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-02-20
              • 1970-01-01
              相关资源
              最近更新 更多