php - 使用php在特定字符后添加一个空格

标签 php html string

我尝试使用谷歌搜索但没有任何帮助。我对 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/

相关文章:

php - 如何使用 Phalcon php Micro 应用程序和自定义路由重定向到索引

php - 带有 PHP 的 PayPal IPN - EOF 异常

php - 使用 CodeIgniter 分页类始终显示上一个和下一个链接

javascript - 当我传递 array 时,它说 "Input data in wrong format for chart type"。 [线形图]

javascript - 如何将表单值发送到服务器、数据库或电子邮件

html - 仅使用 css 在包含的 DIV 中保持多个响应 DIV 的相同高度

c - 如何将 scanf 中的字符串存储在数组中

c++ - 使用 if-else 语句将字符串与常量进行比较。如果/否则跳过第二个

php - 如何在现有的网络项目上实现翻译?

r - 删除分隔符内的字符串部分