php - 如何使用 PHP 删除 wordpress 中的重复空格
问题描述
嗨,今天我有一个字符串,它可以带有重复的空格和字符串,目前我正在使用下面的代码来清理
<?php
$string = ' lorem ipsum dolor s it amet content ';
trim( $string );
解决方案
您可以使用下面的代码清除两侧的空格并删除换行符
<?php
function remove_space( $text ) {
$text = preg_replace('/[\t\n\r\0\x0B]/', '', $text);
$text = preg_replace('/([\s])\1+/', ' ', $text);
$text = trim( $text );
return $text;
}
$exemplo = ' Lorem ipsum dolor sit amet quaqluer dado
test content generates alrel lrea ';
$new_string = remove_space( $exemplo );
echo $new_string;
推荐阅读
- c++ - 多线程 qwebsocket ConnectingState 永远?
- javascript - 如何使用我的 React 应用绕过标签冻结?
- java - 如何使用 android studio 在 Facebook 时间轴字段上上传文本?
- adobe-launch - 如何在控制台选项卡中过滤 adobe 规则?
- php - 如何从 X509 主题公钥信息中提取 pub 部分?
- google-apps-script - 如何重置 Google Drive(文件夹、子文件夹和文件)的权限?
- javascript - React FluentUI TextField - propertychange 事件
- vue.js - 在 symfony 5 上导入 vue 打印代码而不是模板
- typo3 - TYPO3 在另一个扩展中使用来自一个 Extbase-Extension 的对象
- java - 在 WebView 中上传文件