0

I'm using str_replace() PHP function which doesn't replace empty string.

Is it possible to replace it using some other PHP functions?

Here is my code:

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

  $new = "new text";

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

This code really works, but if the second value is empty it doesn't:

  $var = "text1|";

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

  $new = "new text";

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

I want this to echo text1|new text but it doesnt. In the first case it does without a problem. I want this to be changed anyway, it doesn't depend if it's empty or not.

Thanks in advance.

4

1 回答 1

4

在你之后试试这个explode()

if ($expn[1] == '') {
  $var .= 'new text';
} else {
  str_replace($expn[1], $new,  $var);
}
于 2013-08-29T18:18:15.587 回答