Heim  >  Artikel  >  Backend-Entwicklung  >  求一正则表达式,替换某一规则的表情符号为表情图标(<img>表情)

求一正则表达式,替换某一规则的表情符号为表情图标(<img>表情)

WBOY
WBOYOriginal
2016-06-23 13:44:391396Durchsuche

求一正则表达式,替换某一规则的表情符号为表情图标(求一正则表达式,替换某一规则的表情符号为表情图标(<img>表情)表情)

[高兴] 替换为 求一正则表达式,替换某一规则的表情符号为表情图标(<img>表情)  (用中文命名图片规不规范?)

其实最好还是用JS来比较好的。


回复讨论(解决方案)

$str='[高兴]表情图片';
echo preg_replace('/\[(.*)\]/',"求一正则表达式,替换某一规则的表情符号为表情图标(<img>表情)",$str);

这年头,什么都正则...
用中文名做文件名的确不是明智的做法.

function replaceStr($str){
    $str=substr($str,strpos($str,'[')+1,strlen($str)-2);
    return "求一正则表达式,替换某一规则的表情符号为表情图标(<img>表情)";
}

请问,如果这样对整页进行替换,效率怎么样?

$str='[高兴]表情图片';
echo preg_replace('/\[(.*)\]/',"求一正则表达式,替换某一规则的表情符号为表情图标(<img>表情)",$str);

应该还可以接受。

我在网上找到一个JQ插件,在客户端进行替换的,对于[happy]这类的英文表情符号可以替换,但是一旦换到[高兴]中文表情符号就不行,嗯嗯,我也感觉用中文做文件名不好。

// jQuery Plugins: On/Off Emtions var __emotion__cache = false;jQuery.fn.emotions = function() {    var aEmotion = new Array();        aEmotion[21] = new Array('[happy]','\\[happy\\]');        __emotion__cache = true;    this.each(function() {        str = $(this).html();        for(iEM in aEmotion){            reg =  new RegExp(aEmotion[iEM][1],"ig");            str = str.replace(reg,'<span class="plugin_emotion" title="'+encodeURIComponent(aEmotion[iEM][0])+'"><img   src="emotions/'+iEM+'.gif" / alt="求一正则表达式,替换某一规则的表情符号为表情图标(<img>表情)" ></span>');        }        $(this).html(str);    });};jQuery.fn.unemotions = function(){    __emotion__cache = false;    $('.plugin_emotion').each(function(){        $(this).html(decodeURIComponent(this.title));                                       });}

这年头,什么都正则...
用中文名做文件名的确不是明智的做法.

function replaceStr($str){
  $str=substr($str,strpos($str,'[')+1,strlen($str)-2);
  return "求一正则表达式,替换某一规则的表情符号为表情图标(<img>表情)";
}

中文文件名  非常不好  各种不兼容 各种蛋疼。。。

注意看看有木有错误提示

alert(str );
看看是否替换了。


估计是图片名称是中文名称的原因。改用英文吧

那个JQ插件是用0.png、1.png、2.png来替换[face]的
但是把face换成中文就不行了,真奇怪。。。

注意看看有木有错误提示

alert(str );
看看是否替换了。


估计是图片名称是中文名称的原因。改用英文吧

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn