English 中文(简体)
谈判替换 - PHP
原标题:Emoticon Replacement - PHP

我需要将文字幻灯改为html图像标签。 我汇编了以下数据:

private $smile = array(">:]", ":-)", ":)", ":o)", ":]", ":3", ":c)", ":>", "=]", "8)", "=)", ":}", ":^)");
private $laugh = array(">:D", ":-D", ":D", "8-D", "x-D", "X-D", "=-D", "=D", "=-3", "8-)");
private $sad = array(">:[", ":-(", ":(",  ":-c", ":c", ":-<", ":-[", ":[", ":{", ">.>", "<.<", ">.<");
private $wink = array(">;]", ";-)", ";)", "*-)", "*)", ";-]", ";]", ";D", ";^)");
private $tongue = array(">:P", ":-P", ":P", "X-P", "x-p", ":-p", ":p", "=p", ":-Þ", ":Þ", ":-b", ":b", "=p", "=P");
private $surprise = array(">:o", ">:O", ":-O", ":O", "°o°", "°O°", ":O", "o_O", "o.O", "8-0");
private $annoyed = array(">:\", ">:/", ":-/", ":-.", ":\", "=/", "=\", ":S");
private $cry = array(": (", "; (");

private $t_smile = "<img class="smiley" src="/images/emoticons/smile.png"/>";
private $t_laugh = "<img class="smiley" src="/images/emoticons/laugh.png"/>";
private $t_sad = "<img class="smiley" src="/images/emoticons/sad.png"/>";
private $t_wink = "<img class="smiley" src="/images/emoticons/wink.png"/>";
private $t_tongue = "<img class="smiley" src="/images/emoticons/tongue.png"/>";
private $t_surprise = "<img class="smiley" src="/images/emoticons/surprise.png"/>";
private $t_annoyed = "<img class="smiley" src="/images/emoticons/annoyed.png"/>";
private $t_cry = "<img class="smiley" src="/images/emoticons/cry.png"/>"

我目前正在做这样的工作:

$str = str_replace($this->laugh, $this->t_laugh, $str);

每个组别。 这部书行了不起作用,但我需要替换的是,这些字句不是用信件或其他数字环绕的。 换言之,我需要汇编一个包含每个谈判阵列的登记册,以便我能够使用先令,而不是强硬。 难道我可以这样做吗?

EDIT:

此外,我需要配对和取代在扼杀的开始和结束时出现的血迹,因此,用空间技术赢得的简单dding子就足够了。

EDIT:

我沿用了Marks的榜样,先从阵列中选取:

private $smile = "#(^|W)(>:]|:-)|:)|:o)|:]|:3|:c)|:>|=]|8)|=)|:}|:^))($|W)#";
private $laugh = "#(^|W)(>:D|:-D|:D|8-D|x-D|X-D|=-D|=D|=-3|8-)|xD|XD|8D|=3)($|W)#";
private $sad = "#(^|W)(>:[|:-(|:(|:-c|:c|:-<|:-[|:[|:{|>.>|<.<|>.<)($|W)#";
private $wink = "#(^|W)(>;]|;-)|;)|*-)|*)|;-]|;]|;D|;^))($|W)#";
private $tongue = "#(^|W)(>:P|:-P|:P|X-P|x-p|:-p|:p|=p|:-Þ|:Þ|:-b|:b|=p|=P|xp|XP|xP|Xp)($|W)#";
private $surprise = "#(^|W)(>:o|>:O|:-O|:O|°o°|°O°|:O|o_O|o.O|8-0)($|W)#";
private $annoyed = "#(^|W)(>:\|>:/|:-/|:-.|:\|=/|=\|:S|:/)($|W)#";
private $cry = "#(^|W)(: (|; ()($|W)#";

全力以赴地工作!

最佳回答

如果你想使用一种规章:

$pat =  #(^|W) .preg_quote($this->laugh, # ). ($|W)# ;
$str = str_replace($pat, $this->t_laugh, $str);

这基本上意味着谈判可以在提出或提出非言词时开始,随后必须结束书面或另一种非言词性质。 如您的谈判含有任何特殊规范,则有必要使用<>代码>。

此外,更好的形式可能是:

$emoticons = array(
     smile  => array( <img src... , array( >:] , :-) ,...),
     laugh  => array( <img src.... , array(...)),
    ...
)

Then you can loop over everything.


Update

反之,应使用消极的眼光,以对应旁观。 然后,它赢得了与周边空间相匹配的拖车。

<?php
$smile = array(">:]", ":-)", ":)", ":o)", ":]", ":3", ":c)", ":>", "=]", "8)", "=)", ":}", ":^)");
$laugh = array(">:D", ":-D", ":D", "8-D", "x-D", "X-D", "=-D", "=D", "=-3", "8-)");
$sad = array(">:[", ":-(", ":(",  ":-c", ":c", ":-<", ":-[", ":[", ":{", ">.>", "<.<", ">.<");
$wink = array(">;]", ";-)", ";)", "*-)", "*)", ";-]", ";]", ";D", ";^)");
$tongue = array(">:P", ":-P", ":P", "X-P", "x-p", ":-p", ":p", "=p", ":-Ã", ":Ã", ":-b", ":b", "=p", "=P");
$surprise = array(">:o", ">:O", ":-O", ":O", "°o°", "°O°", ":O", "o_O", "o.O", "8-0");
$annoyed = array(">:\", ">:/", ":-/", ":-.", ":\", "=/", "=\", ":S");
$cry = array(": (", "; (");

$ary = array_merge($smile, $laugh, $sad, $wink, $tongue,$surprise,$annoyed,$cry);

foreach ($ary as $a)
{
        $quoted[] = preg_quote($a,  # );
}

$regex = implode( | , $quoted);


$full =  #(?!<w)(  . $regex . )(?!w)# ;
echo $full.PHP_EOL;
$str = "Testing :) emoticons :D :(";

preg_match_all($full, $str, $matches);
print_r($matches[0]);

此外,在撰写reg形时试图使用单一报价,因为双重报价允许绕行顺序,而单一报价则赢得对越航顺序的解释。 i. 有时,在使用双重引言时,你需要加倍大刀切。

问题回答

也许有像样式样的 lo

for($i=0;$i<count($smiles);++$i){
   $smiles[$i]="~s".$smiles[$i]."s~";
}

那么,它只是从表面上消失了(微笑、微笑、文))。

大致如此,或许是你回想的:

function toRegex(array $emotes) {
    foreach ($emotes as &$emote)
        $emote = preg_quote($emote, "/");
    return "/" . implode($emotes, "|") . "/";
}

$imaged = preg_replace(toRegex($smiles), $t_smiles);

此外,如上所述,你最好能拥有一阵列的所有纪念物,而不是你们手工处理的100个小变量。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签