我正在尝试使用php函数解析一个字符串,它将为bbcode样式回显一个<img>
标记:smiley:string。
它确实工作只解析找到的第一个项目,然后停止循环。 我需要它来解析整个文本然后返回它。
我尝试了以下内容:
function parse_emoji($string){
$possibleCodes = array('+1','-1','angry','anguished','astonished','bawling','bleep_bloop','blush','cold_swaet','confounded','confused','cookie','cry','dissapointed','dissapointed_relieved','dizzy','expressionless','fearfull','flushed','frown','grey_question','grimace','grin','heart','heartbreak','hushed','innocent','joy','kiss','kissing_blush','kissing_closed_eyes','kissing_smiling_eyes','kissing_wink','lol','love','love','mask','mrgreen','naughty','neutral','no_mouth','open_mouth','pensive','persevere','rage','relaxed','relieved','scream','skull','sleeping','sleepy','smile','smiley','smirk','star','tongue_grin','tongue_wink','triumph','trollface','unamused','warning','weary','wink','worried','yum');
foreach($possibleCodes as $code) {
return str_replace(':'.$code.':', "<img class='smiley' src='".asset_url()."img/emoticons/".$code.".png'>", $string);
}
}
如果我然后解析$ string('这是一个笑脸:微笑:这个也是:微笑:')它将为第一个标签插入一个图像但是将回显普通:笑脸:为第二个。
答案 0 :(得分:2)
这是因为你在foreach中返回了一个结果。它将停止循环并在仅发生一次循环后返回结果。
您应该删除foreach语句中的返回值:
--debug
答案 1 :(得分:1)
因为你在第一次替换后返回....循环所有单独的替换,然后才返回
function parse_emoji($string){
$possibleCodes = array('+1','-1','angry','anguished','astonished','bawling','bleep_bloop','blush','cold_swaet','confounded','confused','cookie','cry','dissapointed','dissapointed_relieved','dizzy','expressionless','fearfull','flushed','frown','grey_question','grimace','grin','heart','heartbreak','hushed','innocent','joy','kiss','kissing_blush','kissing_closed_eyes','kissing_smiling_eyes','kissing_wink','lol','love','love','mask','mrgreen','naughty','neutral','no_mouth','open_mouth','pensive','persevere','rage','relaxed','relieved','scream','skull','sleeping','sleepy','smile','smiley','smirk','star','tongue_grin','tongue_wink','triumph','trollface','unamused','warning','weary','wink','worried','yum');
foreach($possibleCodes as $code) {
$string = str_replace(':'.$code.':', "<img class='smiley' src='".asset_url()."img/emoticons/".$code.".png'>", $string);
}
return $string;
}