我有这段代码,并希望使用str_replace删除每个尺寸后面的|
旧代码: <?= ($line ["sizes"])."'n"; ?>
<?= $line = "One size"; $sizes = str_replace("|", "", $line); print $line; "'n"; ?>
但是它没有显示像Small Medium Large这样的大小,就像在旧代码中一样。
谢谢。
我不明白你的问题,但如果我只看到句子
我有这段代码,并希望使用str_replace来删除每个尺寸
后面的|
你应该更好地利用explode
和implode
而不是str_replace
:
$sizeStr = "1|2|3";
$sizes = explode("|", $sizeStr); // Array("1", "2", "3");
echo implode("''n", $sizes);
显示
1
2
3
当我运行新代码时,我得到以下输出:
One sizeOne size
我认为你的新代码也可能是这样的。下面是发生的事情:
$line = "One size";
//1. Sets $line equal to "One size" (and prints it because of the short tag.)
$sizes = str_replace("|", "", $line);
//2. Replaces '|' with '' in the string "One size" (it isn't there.)
print $line;
//3. Prints "One size" again.
如果你的旧代码基本上是你想要的(除了打印额外的条),可能你只需要像这样添加str_replace。
<?= (str_replace('|', '', $line["sizes"]))."'n"; ?>