如果还没有空格,如何在子字符串周围添加空格?

时间:2012-09-23 11:46:59

标签: php parsing spaces emoticons

示例输入字符串:test:)test:)test:) :) test:p test
通缉输出字符串:test :) test :) test :) :) test :p test

正如您在上面的示例中所看到的,我想解析字符串并生成一个输出字符串,并在所有预定义的笑脸代码周围添加空格。

我试图通过几个循环来做到这一点,但最终它没有像预期的那样100%工作,而且在大文档上速度非常慢。

所以我想知道用PHP进行这种解析的最佳和最有效的方法是什么?

8 个答案:

答案 0 :(得分:2)

嵌套替换:

preg_replace("/\s\s+/", " ", 
    str_replace( Array( ":)", ":p" ),
        Array( " :) ", " :p " ), $str) );

答案 1 :(得分:1)

<?php

$lspace =   str_replace(":"," :","test:)test:)test:) :) test:p");
$rspace =    str_replace(")",") ",$lspace);
$out    =    preg_replace("/\s\s+/"," ",$rspace);


echo $out;
?>

输出:test :) test :) test :) :) test :p

答案 2 :(得分:0)

相反,正则表达式可以使用str_replace函数。 PHP函数str_replace可以接收数组作为参数,因此您可以尝试:

$input = 'test:)test:)test:) :) test:p test';
$smileys = array( ':)', ':p' );
$smileys_with_spaces = array( ' :)', ' :p' );
$output = str_replace( $smileys_with_spaces, $smileys, $input ); // test:) :) -> test:):)
$output = str_replace( $smileys, $smileys_with_spaces, $input ); // test:):) -> test :) :)

答案 3 :(得分:0)

尝试:

$str = "test:)test:)test:) :) test:p test";
$str = preg_replace('/(?<=[a-z])(?=[^a-z\s])|(?<=[^a-z]{2})(?=[a-z])/', ' ', $str);

答案 4 :(得分:0)

更新并支持N个表情

$smile = array(':)', ':p');

//Escape smiley chars for use in rx
foreach($smile as &$s) {
    $s = preg_quote($s, '/');
}unset($s);

$rx = implode('|', $smile);

$str = 'test:)test:)test:) :) test:p test';

$str = preg_replace('/ *('.$rx.') */', ' $1 ', $str);
$str = preg_replace('/('.$rx.') +/', '$1 ', $str);

echo $str;

答案 5 :(得分:0)

如果您还希望在开头/结尾没有额外的空格,请尝试:

$str = ':):)test:)test:)test:) :) test:p:) test';
$codes = array(
    ':)', ':p', // Add smileys here.
);

// We build the $regexp automatically
$regexp = array();
foreach($codes as $smiley)
    $regexp[] = preg_quote($smiley, '#');
$regexp = implode('|', $regexp);
// Afterwards, we might replace the above with $regexp = 'VERYCOMPLICATEDSYNTAX';

// Split the string at smileys, trimming the smileys as we go,
// and returning an array of smiley-and-non-smiley parts, except for empty ones
$parts = preg_split("#\s*($regexp)\s*#", $str, -1,
    PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

// The new delimiter is a single space.
$str = implode(' ', $parts);

请注意,我使用了空白匹配器\s而不是文字空间;如果您在原始字符串中有换行符,则可能会产生不需要的结果。如果是这种情况,请将“\s”替换为“”文字空格。

答案 6 :(得分:0)

应该有一个更优雅的解决方案,但下面的代码应该可行

$patterns = array();
$replacements = array();

$str = ':) test :)test:) test :)';

$smileys = array(':)', ':p');

foreach($smileys as &$s) {
    $s = preg_quote($s, '/');
}unset($s);
$escaped_smiley = implode('|', $smileys);

$patterns[] = '/([^\s])('.$escaped_smiley.')([^\s])/'; // no spaces
$patterns[] = '/([\s])('.$escaped_smiley.')([^\s])/';  // space on left
$patterns[] = '/([^\s])('.$escaped_smiley.')([\s])/';  // space on right

$patterns[] = '/^('.$escaped_smiley.')([\s])/';   // line starting, space succeding
$patterns[] = '/^('.$escaped_smiley.')([^\s])/';  // line starting, no space
$patterns[] = '/([\s])('.$escaped_smiley.')$/';   // line ending, space preceeding
$patterns[] = '/([^\s])('.$escaped_smiley.')$/';  // line ending, no space

$replacements[] = '$1 $2 $3'; // no spaces 
$replacements[] = '$1$2 $3';  // space on left
$replacements[] = '$1 $2$3';  // space on right

$replacements[] = ' $1$2';    // line starting
$replacements[] = ' $1 $2';   // line starting
$replacements[] = '$1$2 ';    // line ending
$replacements[] = '$1 $2 ';   // line ending


$result = preg_replace($patterns, $replacements, $str);
echo str_replace(' ', '^', $result);

[从@Prinzhorn借来的代码编辑]

答案 7 :(得分:0)

如果你还没有解决问题,我想出了一个可行的解决方案。首先用空格作为分隔符来爆炸字符串,用额外的空格替换表情符号并用内爆来重新定义它们。

检查php fiddle是否有实施。