用PHP替换多余的空格和换行符?

|
$string = \"My    text       has so    much   whitespace    




Plenty of    spaces  and            tabs\";

echo preg_replace(\"/\\s\\s+/\", \" \", $string);
我阅读了PHP \的文档并遵循preg_replace \的教程,但是此代码产生了   我的文字有很多空格很多空格和制表符 如何将其转换为:   我的文字有很多空白   大量的空格和制表符     
已邀请:
        首先,我想指出的是,根据操作系统的不同,新行可以是\\ r,\\ n或\\ r \\ n。 我的解决方案:
echo preg_replace(\'/[ \\t]+/\', \' \', preg_replace(\'/[\\r\\n]+/\', \"\\n\", $string));
如有必要,可以分为两行:
$string = preg_replace(\'/[\\r\\n]+/\', \"\\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(?:[\\v\\h]+)/\', \'\', $string);
    
        替换多个换行符,制表符,空格
$text = preg_replace(\"/[\\r\\n]+/\", \"\\n\", $text);
$text = preg_replace(\"/\\s+/\", \' \', $text);
经过测试:)     
        
//Newline and tab space to single space

$from_mysql = str_replace(array(\"\\r\\n\", \"\\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.
    
        替代方法:
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
编辑:读这是因为它是一种不同的方法。它可能不是要求的内容,但至少不会合并不同空格的组(例如,“ 10”会变成“ 11”)。     
        这样可以完全压缩整个字符串(例如大型博客文章),同时保留所有HTML标签。
$email_body = str_replace(PHP_EOL, \' \', $email_body);
    //PHP_EOL = PHP_End_Of_Line - would remove new lines too
$email_body = preg_replace(\'/[\\r\\n]+/\', \"\\n\", $email_body);
$email_body = preg_replace(\'/[ \\t]+/\', \' \', $email_body);
    
        你为什么这样做? 即使您使用了多个空格,html也只显示一个空格... 例如:
<i>test               content 1       2 3 4            5</i>
输出将是: 测试内容1 2 3 4 5 如果您在html中需要多个空格,则必须使用
&nbsp;
    
        尝试:
$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); 
    

bab

        不知道这是否有用,我也不是绝对肯定它会像它应该的那样工作,但似乎对我有用。 清除多个空格以及任何您想要或不想要的任何内容的函数,并生成单行字符串或多行字符串(取决于传递的参数/选项)。也可以删除或保留其他语言的字符,并将换行符转换为空格。
/** ¯\\_(ツ)_/¯ 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[] = \"/(?<!^)(?:[^\\r\\na-z0-9][\\t]+)/m\";
        $patterns[] = \"/[ ]+(?![a-z0-9$options])|[^a-z0-9$options\\s]/im$l\";
        $patterns[] = \"/\\t/m\";
        $patterns[] = \"/(?<!^)$tabSpaces/m\";
        $replacements[] = \" \";
        $replacements[] = \"\";
        $replacements[] = $tabSpaces;
        $replacements[] = \" \";
    } else if ($multiLine === null) {
        $patterns[] = \"/[\\r\\n\\t]+/m\";
        $patterns[] = \"/[^a-z0-9$options]/im$l\";
        $replacements = \"\";
    } else {
        $patterns[] = \"/[\\r\\n\\t]+/m\";
        $patterns[] = \"/[ ]+(?![a-z0-9$options\\t])|[^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);
}
用法示例: 回声标题(\'Content-Type:text / html; charset = utf-8 \',true); $ string = \“ fjl!sj \\ nfl _ sfjs-lkjf \\ r \\ n \\ tskj女性与环境健康fsl \\ tklkj \\ thl jhj⚧

要回复问题请先登录注册