在字符串之前和之后添加 [英] Add before and after the string
本文介绍了在字符串之前和之后添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$variable = 'for linebreak add 2 spaces at end';
每次更改此变量的值。
如何在此字符串前后添加一些文本或html?
例如如果我们要在前面添加'< div>',在其后添加
'< / div>'
,则字符串应
E.g. if we want to add '<div>'
before and '</div>'
after, string should look like:
$variable = '<div>for linebreak add 2 spaces at end</div>';
推荐答案
Marko的解决方案是简单案例的解决之道。如果需要连接多个字符串,则据说连接数组要快得多。
Marko's solution is the way to go for simple cases. If you need to concatenate many strings, it is said that joining arrays is much faster.
$string[]='<div>';
$string[]= $variable;
$string[]='</div>';
$string = join('',$string);
这篇关于在字符串之前和之后添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文