没有循环
function smilies($string)
{
$emote = array(':)', ':(', ':P', ':D' , '^_^', ';)', ':3', ':*', '<3' );
$emote_replace = array(
'<img title=":)" class="emote_img" src="/public/img/blank.gif" style="background-position: 0px;" >',
'<img title=":(" class="emote_img" src="/public/img/blank.gif" style="background-position: -16px;" >',
'<img title=":P" class="emote_img" src="/public/img/blank.gif" style="background-position: -31px;" >',
'<img title=":D" class="emote_img" src="/public/img/blank.gif" style="background-position: -46px;" >',
'<img title="^_^" class="emote_img" src="/public/img/blank.gif" style="background-position: -239px;" >',
'<img title=";)" class="emote_img" src="/public/img/blank.gif" style="background-position: -79px;" >',
'<img title=":3" class="emote_img" src="/public/img/blank.gif" style="background-position: -319px;" >',
'<img title=":*" class="emote_img" src="/public/img/blank.gif" style="background-position: -208px;" >',
'<img title="<3" class="emote_img" src="/public/img/blank.gif" style="background-position: -224px;" >',
);
return str_replace($emote, $emote_replace, $string);
}
我想替换:p ,:P 而不向$ emote_replace和添加新行;(和:' ((这是哭的fb快捷方式)
答案 0 :(得分:0)
您可以忽略正则表达式的情况(使用循环创建第一个数组可能更简单):
function smilies($string) {
$emote = array(
'/'.preg_quote(':)').'/i',
'/'.preg_quote(':(').'/i',
'/'.preg_quote(':P').'/i',
'/'.preg_quote(':D').'/i',
'/'.preg_quote('^_^').'/i',
'/'.preg_quote(';)').'/i',
'/'.preg_quote(':3').'/i',
'/'.preg_quote(':*').'/i',
'/'.preg_quote('<3').'/i'
);
$emote_replace = array(
'<img title=":)" class="emote_img" src="/public/img/blank.gif" style="background-position: 0px;" >',
'<img title=":(" class="emote_img" src="/public/img/blank.gif" style="background-position: -16px;" >',
'<img title=":P" class="emote_img" src="/public/img/blank.gif" style="background-position: -31px;" >',
'<img title=":D" class="emote_img" src="/public/img/blank.gif" style="background-position: -46px;" >',
'<img title="^_^" class="emote_img" src="/public/img/blank.gif" style="background-position: -239px;" >',
'<img title=";)" class="emote_img" src="/public/img/blank.gif" style="background-position: -79px;" >',
'<img title=":3" class="emote_img" src="/public/img/blank.gif" style="background-position: -319px;" >',
'<img title=":*" class="emote_img" src="/public/img/blank.gif" style="background-position: -208px;" >',
'<img title="<3" class="emote_img" src="/public/img/blank.gif" style="background-position: -224px;" >',
);
return preg_replace($emote, $emote_replace, $string);
}
答案 1 :(得分:0)
只需使用str_ireplace
更改str_replace
即可。这是str_replace
的不区分大小写的版本。
答案 2 :(得分:0)
我修改了sebcap26的功能
<?php
function smilies($string) {
$emote = array(
'/([:][-]?[)])/i', //:) or :-)
'/([:][-]?[(])/i', //:( or :-(
'/([:][-]?[P])/i', //:p or :P or :-p or :-P
'/([:][-]?[D])/i', //:d or :D or :-d or :-D
'/([^][-]?[^])/i', //^^ or ^-^
'/([;][-]?[)])/i', //;) or ;-)
'/'.preg_quote(':3').'/i',
'/'.preg_quote(':*').'/i',
'/'.preg_quote('<3').'/i'
);
$emote_replace = array(
'<img title=":)" class="emote_img" src="/public/img/blank.gif" style="background-position: 0px;" >',
'<img title=":(" class="emote_img" src="/public/img/blank.gif" style="background-position: -16px;" >',
'<img title=":P" class="emote_img" src="/public/img/blank.gif" style="background-position: -31px;" >',
'<img title=":D" class="emote_img" src="/public/img/blank.gif" style="background-position: -46px;" >',
'<img title="^_^" class="emote_img" src="/public/img/blank.gif" style="background-position: -239px;" >',
'<img title=";)" class="emote_img" src="/public/img/blank.gif" style="background-position: -79px;" >',
'<img title=":3" class="emote_img" src="/public/img/blank.gif" style="background-position: -319px;" >',
'<img title=":*" class="emote_img" src="/public/img/blank.gif" style="background-position: -208px;" >',
'<img title="<3" class="emote_img" src="/public/img/blank.gif" style="background-position: -224px;" >',
);
return preg_replace($emote, $emote_replace, $string);
}
?>
我改变了第一个的正则表达式来覆盖它可能出现' - '(hiphen)的情况,把它作为可选项。