0
我有一个变量可以很早地追加到字符串中,但是如果符合条件,我需要用空字符串替换它(该条件只能在稍后确定在代码中)。在字符串末尾用“”替换下面的文本
例如:
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul role=\"menu\">\n";
我现在需要更换什么获取附加到$output
串在这里与一个空字符串。这在其他地方完成,但我仍然可以访问$ indent变量,所以我知道已经添加了多少“\ t”。
所以,我知道我可以使用preg_match
和preg_replace
像这样做:
if (preg_match("/\n$indent<ul role=\"menu\">\n$/", $output))
$output = preg_replace("/\n$indent<ul role=\"menu\">\n$/", "", $output);
else
$output .= "$indent</ul>\n";
但我在这里想上的表现,如果有更好的方法来做到这一点?如果有人可以用我的确切$output
提供一个新行和制表符的例子,那就太好了。
,看起来不错,所有,但如何使用''/ N','/ t'时生效strlen'输出,和/或'/ r'? –
它可以工作。 'strlen('\ n \ n')'是4. – BeetleJuice
非常感谢! –