繁体   English   中英

是否可以在PHP中用自定义文本替换空字符串?

[英]Is it possible to replace empty string with custom text in PHP?

我正在使用str_replace()PHP函数,该函数不会替换空字符串。

是否可以使用其他一些PHP函数替换它?

这是我的代码:

  $var = "text1|text2";
  $expn = explode("|", $var);

  $new = "new text";

  str_replace($expn[1], $new,  $var);

该代码确实有效,但是如果第二个值为空,则不会:

  $var = "text1|";

  $expn = explode("|", $var);

  $new = "new text";

  str_replace($expn[1], $new,  $var);

我想让它回显text1 | new文本,但不是。 在第一种情况下,它没有问题。 我想无论如何都要更改它,这不取决于它是否为空。

提前致谢。

explode()之后尝试一下:

if ($expn[1] == '') {
  $var .= 'new text';
} else {
  str_replace($expn[1], $new,  $var);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM