1

URL 内のスマイリーを解析しないように BBcode パーサーを調整するにはどうすればよいでしょうか?

これが私のパーサーです:

    $smilies = array(   
"><" => '<img src="/jscripts/sce/emoticons/angry.png" alt="" />',
":'(" => '<img src="/jscripts/sce/emoticons/cry.png" alt="" />',
":S" => '<img src="/jscripts/sce/emoticons/dizzy.png" alt="" />',
":D" => '<img src="/jscripts/sce/emoticons/grin.png" alt="" />',
"^_^" => '<img src="/jscripts/sce/emoticons/happy.png" alt="" />',
"<3" => '<img src="/jscripts/sce/emoticons/heart.png" alt="" />',
":huh:" => '<img src="/jscripts/sce/emoticons/huh.png" alt="" />',
":|" => '<img src="/jscripts/sce/emoticons/pouty.png" alt="" />',
":(" => '<img src="/jscripts/sce/emoticons/sad.png" alt=""/>',
":O" => '<img src="/jscripts/sce/emoticons/shocked.png" alt="" />',
":sick:" => '<img src="/jscripts/sce/emoticons/sick.png" alt="" />',
":)" => '<img src="/jscripts/sce/emoticons/smile.png" alt="" />',
":P" => '<img src="/jscripts/sce/emoticons/tongue.png" alt="" />',
":S" => '<img src="/jscripts/sce/emoticons/unsure.png" alt="" />',
":woot:" => '<img src="/jscripts/sce/emoticons/w00t.png" alt="" />',
":whistle:" => '<img src="/jscripts/sce/emoticons/whistle.png" alt="" />',
";)" => '<img src="/jscripts/sce/emoticons/wink.png" alt="" />',
":wub:" => '<img src="/jscripts/sce/emoticons/wub.png" alt="" />'
);

$body = str_replace( array_keys( $smilies ), array_values( $smilies ), $body );

誰かがのリンクを入れたときに問題が発生しました

http://pcgamingwiki.com/wiki/User:Soeb

次に、「:S」スマイリー画像を入れようとしたのはどれですか?

4

1 に答える 1

2

代わりにpreg_replace()を使用して、スマイリーがスペース(または文字列の開始、または文字列の終了) でstr_replace()囲まれているかどうかを確認できます。

コード:

$smilies = array(   
"/( |^)><( |$)/" => ' <img src="/jscripts/sce/emoticons/angry.png" alt="" /> ',
"/( |^):'\(( |$)/" => ' <img src="/jscripts/sce/emoticons/cry.png" alt="" /> ',
"/( |^):S( |$)/" => ' <img src="/jscripts/sce/emoticons/dizzy.png" alt="" /> ',
"/( |^):D( |$)/" => ' <img src="/jscripts/sce/emoticons/grin.png" alt="" /> ',
"/( |^)\^_\^( |$)/" => ' <img src="/jscripts/sce/emoticons/happy.png" alt="" /> ',
"/( |^)<3( |$)/" => ' <img src="/jscripts/sce/emoticons/heart.png" alt="" /> ',
"/( |^):huh:( |$)/" => ' <img src="/jscripts/sce/emoticons/huh.png" alt="" /> ',
"/( |^):\|( |$)/" => ' <img src="/jscripts/sce/emoticons/pouty.png" alt="" /> ',
"/( |^):\(( |$)/" => ' <img src="/jscripts/sce/emoticons/sad.png" alt=""/> ',
"/( |^):O( |$)/" => ' <img src="/jscripts/sce/emoticons/shocked.png" alt="" /> ',
"/( |^):sick:( |$)/" => ' <img src="/jscripts/sce/emoticons/sick.png" alt="" /> ',
"/( |^):\)( |$)/" => ' <img src="/jscripts/sce/emoticons/smile.png" alt="" /> ',
"/( |^):P( |$)/" => ' <img src="/jscripts/sce/emoticons/tongue.png" alt="" /> ',
"/( |^):S( |$)/" => ' <img src="/jscripts/sce/emoticons/unsure.png" alt="" /> ',
"/( |^):woot:( |$)/" => ' <img src="/jscripts/sce/emoticons/w00t.png" alt="" /> ',
"/( |^):whistle:( |$)/" => ' <img src="/jscripts/sce/emoticons/whistle.png" alt="" /> ',
"/( |^);\)( |$)/" => ' <img src="/jscripts/sce/emoticons/wink.png" alt="" /> ',
"/( |^):wub:( |$)/" => ' <img src="/jscripts/sce/emoticons/wub.png" alt="" /> '
);

$body=preg_replace( array_keys($smilies), array_values($smilies), $body );

実際の動作はこちらでご覧ください。

于 2013-03-23T11:59:46.097 に答える