示例输入字符串:test:)test:)test:) :) test:p test
想要的输出字符串:test :) test :) test :) :) test :p test
正如您在上面的示例中所看到的,我想解析字符串并生成一个输出字符串,并在所有预定义的笑脸代码周围添加空格。
我试图用几个循环来做到这一点,但最终它并没有像预期的那样 100% 工作,而且在大文档上速度非常慢。
所以我想知道用 PHP 进行这种解析的最好和最有效的方法是什么?
嵌套替换:
preg_replace("/\s\s+/", " ",
str_replace( Array( ":)", ":p" ),
Array( " :) ", " :p " ), $str) );
<?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
如果您还没有解决问题,我想出了一个可行的解决方案。首先用空格作为分隔符来分解字符串,用额外的空格替换笑脸,然后用 implode 重新组合它们。
检查php fiddle的实现。
应该有一个更优雅的解决方案,但下面的代码应该可以工作
$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 借来的代码进行编辑]
相反,您可以使用正则表达式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 :) :)
尝试:
$str = "test:)test:)test:) :) test:p test";
$str = preg_replace('/(?<=[a-z])(?=[^a-z\s])|(?<=[^a-z]{2})(?=[a-z])/', ' ', $str);
更新支持 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;
If you also want to not have extra spaces at the beginning/end, try this:
$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);
Note that I have used the whitespace matcher \s
instead of a literal space; this can have unwanted results if you have line feeds in the original string. If that is the case, replace "\s
" with a " " literal space.