我尝试使用谷歌搜索但没有任何帮助。我对 PHP 没有经验。我希望问题很清楚。
所以我想要的是在字符串中的特定字符后面放一个空格
喜欢来自:
1234b1
到:
1234b 1
我需要一个代码来帮我做这件事
注意:不需要用勺子喂我。
最佳答案
一个相当优雅的方法是使用 regex :
$input = '1234b1';
$pattern = '/b/';
$replacement = '$0 ';
$output = preg_replace($pattern,$replacement,$input);
使用 PHP 的交互式 shell 运行它:
$ php -a
Interactive mode enabled
php > $input = '1234b1';
php > $pattern = '/b/';
php > $replacement = '$0 ';
php > $output = preg_replace($pattern,$replacement,$input);
php > echo $output;
1234b 1
编辑:如果您想跳过一行,请更新 $replacement
与 "\$0\n"
,或者如果你想要 HTML 新行:$0<br>
:
$input = 'abbbbasasjscxxxxc';
$pattern = '/c/';
$replacement = "\$0\n"; //'$0<br>' for HTML
$output = preg_replace($pattern,$replacement,$input);
echo $output;
关于php - 使用php在特定字符后添加一个空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472869/