5

示例输入字符串:test:)test:)test:) :) test:p test
想要的输出字符串:test :) test :) test :) :) test :p test

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

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

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

4

8 回答 8

2

嵌套替换:

preg_replace("/\s\s+/", " ", 
    str_replace( Array( ":)", ":p" ),
        Array( " :) ", " :p " ), $str) );
于 2012-09-23T12:01:14.923 回答
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

于 2012-09-27T07:04:21.007 回答
0

如果您还没有解决问题,我想出了一个可行的解决方案。首先用空格作为分隔符来分解字符串,用额外的空格替换笑脸,然后用 implode 重新组合它们。

检查php fiddle的实现。

于 2012-09-26T10:44:11.497 回答
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 借来的代码进行编辑]

于 2012-09-23T13:14:13.600 回答
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 :) :)
于 2012-09-23T11:57:14.753 回答
0

尝试:

$str = "test:)test:)test:) :) test:p test";
$str = preg_replace('/(?<=[a-z])(?=[^a-z\s])|(?<=[^a-z]{2})(?=[a-z])/', ' ', $str);
于 2012-09-23T11:59:15.503 回答
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;
于 2012-09-23T12:01:08.183 回答
0

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.

于 2012-09-23T13:08:53.537 回答