php - Cannot change text to image use preg_replace -
php - Cannot change text to image use preg_replace -
i trying convert texts in string emoticons. have tried , succeeded half way.
problem: when replacing characters in string emoticons, characters such ":/" in link http://mywebsite.com gets replaced.
what did till now:
chat.php
<?php require('common.php'); if($_post && !empty($_post['title'])){ $result = $db->add_news($_post['title']); } $text=""; function parsesmiley($text){ //smiley image $smileys=array('o:)'=>'angel.gif', ':3'=>'colonthree.gif', 'o.o'=>'confused.gif', ":'("=>'cry.gif', '3:)'=>'devil.gif', ':('=>'frown.gif', ':o'=>'gasp.gif', '8)'=>'glasses.gif', ':d'=>'grin.gif', ">:-("=>'grumpy.gif', '<3'=>'heart.gif', '^_^'=>'kiki.gif', ':*'=>'kiss.gif', ':v'=>'pacman.gif', ':)'=>'smile.gif', '-_-'=>'squint.gif', '8|'=>'sunglasses.gif', ':p'=>'tongue.gif', ':/'=>'unsure.gif', '>:-o'=>'upset.gif', ';)'=>'wink.gif'); //now need find , replace foreach($smileys $smiley=>$img){ $text=str_replace(htmlspecialchars($smiley), "<img src='emotions-fb/{$img}'/>", $text); } //now homecoming homecoming $text; } ?> db.php
function get_news(){ if($result = $this->db->query('select * news id<>1 order add_date desc limit 50')){ $return = ''; while($r = $result->fetch_object()){ $timing=explode(" ", $r->add_date); $return .= '<p>'.wordwrap(htmlspecialchars($r->title), 25, "\n", true).'</p>'; //$return .='<p>'.$timing[1].' on '.$timing[0].'</p>'; $return .= '<hr/>'; } homecoming $return; } } after searched on stackoverflow , got doesnt replace text image
foreach($smileys $smiley=>$img){ $smiley=preg_quote($smiley); $text=preg_replace("~\b$smiley\b~", "<img src='emotions-fb/{$img}'/>", $text); } i dont know going wrong. in advance.
try:
$smileys=array('o:)'=>'angel.gif', ':3'=>'colonthree.gif', 'o.o'=>'confused.gif', ":'("=>'cry.gif', '3:)'=>'devil.gif', ':('=>'frown.gif', ':o'=>'gasp.gif', '8)'=>'glasses.gif', ':d'=>'grin.gif', ">:-("=>'grumpy.gif', '<3'=>'heart.gif', '^_^'=>'kiki.gif', ':*'=>'kiss.gif', ':v'=>'pacman.gif', ':)'=>'smile.gif', '-_-'=>'squint.gif', '8|'=>'sunglasses.gif', ':p'=>'tongue.gif', ':/'=>'unsure.gif', '>:-o'=>'upset.gif', ';)'=>'wink.gif'); $text='some illustration text smiling :/ ^_^ , :\'( , link <a href="http://google.com"> site :/</a>'; foreach ($smileys $smiley => $img) { $smiley = preg_quote( $smiley ); $text = preg_replace( "#(?<=\s|^)(?:$smiley)(?=\s|$)?#i", '<img src="emotions-fb/'. $img .'">', $text); } echo $text; php preg-replace str-replace
Comments
Post a Comment