$string = "My text has so much whitespace
Plenty of spaces and tabs";
echo preg_replace("/ss+/", " ", $string);
我阅读了 PHP 的文档并遵循了preg_replace()
教程,但是这段代码会产生:
My text has so much whitespace Plenty of spaces and tabs
我怎样才能把它变成:
My text has so much whitespace
Plenty of spaces and tabs
首先,我想指出,新行可以是 \r、 或 \r,具体取决于操作系统。
我的解决方案:
echo preg_replace('/[ t]+/', ' ', preg_replace('/[rn]+/', "n", $string));
如有必要,可以分成 2 行:
$string = preg_replace('/[rn]+/', "n", $string);
echo preg_replace('/[ t]+/', ' ', $string);
更新:
更好的解决方案是这个:
echo preg_replace('/[ t]+/', ' ', preg_replace('/s*$^s*/m', "n", $string));
或:
$string = preg_replace('/s*$^s*/m', "n", $string);
echo preg_replace('/[ t]+/', ' ', $string);
我更改了使多行换行符更好地的正则表达式。它使用"m"修饰符(使 ^ 和 $ 匹配新行的开头和结尾(,并删除任何 \s(空格、制表符、换行符、换行符(字符,这些字符是字符串的结尾和下一个字符串的开头。这解决了只有空格的空行问题。在我之前的例子中,如果一行充满了空格,它就会跳过额外的一行。
编辑了正确答案。从 PHP 5.2.4 左右开始,以下代码就可以了:
echo preg_replace('/v(?:[vh]+)/', '', $string);
替换多个换行符、制表符、空格
$text = preg_replace("/[rn]+/", "n", $text);
$text = preg_replace("/s+/", ' ', $text);
经测试:)
//Newline and tab space to single space
$from_mysql = str_replace(array("rn", "r", "n", "t"), ' ', $from_mysql);
// Multiple spaces to single space ( using regular expression)
$from_mysql = ereg_replace(" {2,}", ' ',$from_mysql);
// Replaces 2 or more spaces with a single space, {2,} indicates that you are looking for 2 or more than 2 spaces in a string.
完全缩小整个字符串(例如大型博客文章(,同时保留所有HTML标签。
$email_body = str_replace(PHP_EOL, ' ', $email_body);
//PHP_EOL = PHP_End_Of_Line - would remove new lines too
$email_body = preg_replace('/[rn]+/', "n", $email_body);
$email_body = preg_replace('/[ t]+/', ' ', $email_body);
替代方法:
echo preg_replace_callback("/s+/", function ($match) {
$result = array();
$prev = null;
foreach (str_split($match[0], 1) as $char) {
if ($prev === null || $char != $prev) {
$result[] = $char;
}
$prev = $char;
}
return implode('', $result);
}, $string);
输出:
My text has so much whitespace
Plenty of spaces and tabs
编辑:重新添加了它,因为它是一种不同的方法。这可能不是所要求的,但它至少不会合并不同空格的组(例如 space, tab, tab, space, nl, nl, space, space
会变成space, tab, space, nl, space
(。
将回显数据从PHP传递到Javascript(格式化为JSON(时遇到同样的问题。该字符串充斥着无用的 \r 和 \t 字符,这些字符既不需要也不显示在页面上。
我最终使用的解决方案是另一种回声方式。与preg_replace相比,这节省了大量的服务器资源(正如这里的其他人所建议的那样(。
以下是前后对比:
以前:
echo '
<div>
Example
Example
</div>
';
输出:
后:
echo
'<div>',
'Example',
'Example',
'</div>';
输出:
(是的,您不仅可以用点连接回声,还可以用逗号连接回声。
你为什么要这样做?
HTML 只显示一个空格,即使您使用多个空格...
例如:
<i>test content 1 2 3 4 5</i>
输出将是:
测试内容 1 2 3 4 5
如果在 HTML 中需要多个空格,则必须使用
尝试:
$string = "My text has so much whitespace
Plenty of spaces and tabs";
//Remove duplicate newlines
$string = preg_replace("/[n]*/", "n", $string);
//Preserves newlines while replacing the other whitspaces with single space
echo preg_replace("/[ t]*/", " ", $string);
此任务要求将连续空格和制表符("水平空格" -- h
(替换为单个文字空格,并将连续的回车符和换行符("垂直空格" -- v
(替换为换行符。 要确保在您自己的系统中使用适当的换行符序列,请使用 PHP_EOL
。
匹配最少为零的出现次数(带有*
(是没有意义的,因为您可能会在以前没有空格字符的地方添加一个空格字符。 因此,此任务的模式应仅使用+
(一个或多个(量词。
如果字符串的开头或结尾出现任何类型的空格的可能性,请不要费心用正则表达式删除它们,只需使用 trim()
即可。
在这种情况下,R
将提供与v
相同的结果,但R
走得更远,更复杂(也许不必要(。这是一本内容丰富的读物:https://www.npopov.com/2011/12/10/PCRE-and-newlines.html#meet-r
代码:(演示(
$string = "
My text has so much whitespace
Plenty of spaces and tabs ";
var_export(
preg_replace(
['/h+/', '/v+/'],
[' ', PHP_EOL],
trim($string)
)
);
输出:
'My text has so much whitespace
Plenty of spaces and tabs'
不确定这是否有用,也不是绝对肯定它应该工作,但它似乎对我有用。
一个函数,它清除多个空格以及您想要或不需要的任何其他内容,并生成单行字符串或多行字符串(取决于传递的参数/选项(。还可以删除或保留其他语言的字符,并将换行符转换为空格。
/** ¯_(ツ)_/¯ Hope it's useful to someone. **/
// If $multiLine is null this removes spaces too. <options>'[:emoji:]' with $l = true allows only known emoji.
// <options>'[:print:]' with $l = true allows all utf8 printable chars (including emoji).
// **** TODO: If a unicode emoji or language char is used in $options while $l = false; we get an odd � symbol replacement for any non-matching char. $options char seems to get through, regardless of $l = false ? (bug (?)interesting)
function alphaNumericMagic($value, $options = '', $l = false, $multiLine = false, $tabSpaces = " ") {
$utf8Emojis = '';
$patterns = [];
$replacements = [];
if ($l && preg_match("~([:emoji:])~", $options)) {
$utf8Emojis = [
'x{1F600}-x{1F64F}', /* Emoticons */
'x{1F9D0}-x{1F9E6}',
'x{1F300}-x{1F5FF}', /* Misc Characters */ // x{1F9D0}-x{1F9E6}
'x{1F680}-x{1F6FF}', /* Transport and Map */
'x{1F1E0}-x{1F1FF}' /* Flags (iOS) */
];
$utf8Emojis = implode('', $utf8Emojis);
}
$options = str_replace("[:emoji:]", $utf8Emojis, $options);
if (!preg_match("~([:graph:]|[:print:]|[:punct:]|\-)~", $options)) {
$value = str_replace("-", ' ', $value);
}
if ($l) {
$l = 'u';
$options = $options . 'p{L}p{N}p{Pd}';
} else { $l = ''; }
if (preg_match("~([:print:])~", $options)) {
$patterns[] = "/[ ]+/m";
$replacements[] = " ";
}
if ($multiLine) {
$patterns[] = "/(?<!^)(?:[^rna-z0-9][t]+)/m";
$patterns[] = "/[ ]+(?![a-z0-9$options])|[^a-z0-9$optionss]/im$l";
$patterns[] = "/t/m";
$patterns[] = "/(?<!^)$tabSpaces/m";
$replacements[] = " ";
$replacements[] = "";
$replacements[] = $tabSpaces;
$replacements[] = " ";
} else if ($multiLine === null) {
$patterns[] = "/[rnt]+/m";
$patterns[] = "/[^a-z0-9$options]/im$l";
$replacements = "";
} else {
$patterns[] = "/[rnt]+/m";
$patterns[] = "/[ ]+(?![a-z0-9$optionst])|[^a-z0-9$options ]/im$l";
$replacements[] = " ";
$replacements[] = "";
}
echo "n";
print_r($patterns);
echo "n";
echo $l;
echo "n";
return preg_replace($patterns, $replacements, $value);
}
用法示例:
echo header('Content-Type: text/html; charset=utf-8', true);
$string = "fjl!sjnfl _ sfjs-lkjfrntskj 婦女與環境健康 fsl tklkjthl jhj ⚧😄 lkj ⸀ skjfl gwo lsjowgtfls s";
echo "<textarea style='width:100%; height:100%;'>";
echo alphaNumericMagic($string, '⚧', true, null);
echo "nnANDnn";
echo alphaNumericMagic($string, '[:print:]', true, true);
echo "</textarea>";
结果:
fjlsjflsfjslkjfskj婦女與環境健康fslklkjhljhj⚧lkjskjflgwolsjowgtflss
AND
fjl!sj
fl _ sfjs-lkjf
skj 婦女與環境健康 fsl klkj hl jhj ⚧😄 lkj ⸀ skjfl gwo lsjowgtfls s