Page 1 of 1

another str_replace problem

Posted: Tue Jun 04, 2002 5:32 pm
by vio

Code: Select all

<?php 

$img_dir = "http://www.neo-net.org/~test1/images/smileys";

// $string is whatever text you want to replace =) with the image is 
$string = str_replace(' :) ',"<img src="$img_dir/smile.gif">", $string); 
$string = str_replace(' :( ',"<img src="$img_dir/frown.gif">", $string);
$string = str_replace(' ;) ',"<img src="$img_dir/wink.gif">", $string);
$string = str_replace(' :P ',"<img src="$img_dir/tongue.gif">", $string);
$string = str_replace(' :D ',"<img src="$img_dir/bigsmile.gif">", $string);
$string = str_replace(' :&#1111; ',"<img src="$img_dir/mad.gif">", $string);
$string = str_replace(' ^_^ ',"<img src="$img_dir/yay.gif">", $string);
$string = str_replace(' -.- ',"<img src="$img_dir/ugh.gif">", $string);
$string = str_replace(' x_X ',"<img src="$img_dir/xed.gif">", $string);
$string = str_replace(' ~_~ ',"<img src="$img_dir/bah.gif">", $string);
$string = str_replace(' :* ',"<img src="$img_dir/flirt.gif">", $string);
$string = str_replace(' ;( ',"<img src="$img_dir/sigh.gif">", $string);
$string = str_replace(' -_- ',"<img src="$img_dir/annoyed.gif">", $string);
$string = str_replace(' :o ',"<img src="$img_dir/gasp.gif">", $string);
$string = str_replace(' :\ ',"<img src="$img_dir/hm.gif">", $string);
$string = str_replace(' ._. ',"<img src="$img_dir/beady.gif">", $string);
$string = str_replace(' 0:) ',"<img src="$img_dir/angel.gif">", $string);
$string = str_replace(' :x ',"<img src="$img_dir/mouthshut.gif">", $string);
$string = str_replace(' >.< ',"<img src="$img_dir/yuck.gif">", $string);
$string = str_replace(' :yawn: ',"<img src="$img_dir/yawn.gif">", $string);
$string = str_replace(' (@) ',"<img src="$img_dir/hypnotized.gif">", $string);
$string = str_replace(' :sword: ',"<img src="$img_dir/sword.gif">", $string);
$string = str_replace(' :thumbs: ',"<img src="$img_dir/thumbsup.gif">", $string);
$string = str_replace(' :yinyang: ',"<img src="$img_dir/yinyang.gif">", $string);
echo $string; 

?>
i put that at the very top part of my index.php page and when i try putting a smiley (text) it doesnt convert to an image.....

Posted: Tue Jun 04, 2002 7:44 pm
by sam
What is string equal to. Is that the variable that your sending to the script? are you using php >= 4.1.x?

Cheers Sam

Posted: Tue Jun 04, 2002 9:11 pm
by Shadough
Hello, I could be wrong - but I think you could do

Code: Select all

$str .= str_replace (...);
$str .= str_replace (...);
The "." assigns $str more values that way. :D

Posted: Tue Jun 04, 2002 9:14 pm
by jason
Shadough: No, as that only concatenates the strings together. In this case, he wants to replace the string, or overwrite it.

Posted: Tue Jun 04, 2002 10:26 pm
by Shadough
This should work:

Code: Select all

<?php 

convertSmilie($user_input, "http://www.neo-net.org/~test1/images/smileys");

function convertSmilie($user_input, $img_dir) &#123;
  $string = array();
  $strings&#1111;] = array(':)',"<img src="$img_dir/smile.gif">");
  $strings&#1111;] = array(':(',"<img src="$img_dir/frown.gif">");
  $strings&#1111;] = array(';)',"<img src="$img_dir/wink.gif">");
  $strings&#1111;] = array(':P',"<img src="$img_dir/tongue.gif">");
  $strings&#1111;] = array(':D',"<img src="$img_dir/bigsmile.gif">");
  $strings&#1111;] = array(':&#1111;',"<img src="$img_dir/mad.gif">");
  $strings&#1111;] = array('^_^',"<img src="$img_dir/yay.gif">");
  $strings&#1111;] = array('-.-',"<img src="$img_dir/ugh.gif">");
  $strings&#1111;] = array('x_X',"<img src="$img_dir/xed.gif">");
  $strings&#1111;] = array('~_~',"<img src="$img_dir/bah.gif">");
  $strings&#1111;] = array(':*',"<img src="$img_dir/flirt.gif">");
  $strings&#1111;] = array(';(',"<img src="$img_dir/sigh.gif">");
  $strings&#1111;] = array('-_-',"<img src="$img_dir/annoyed.gif">");
  $strings&#1111;] = array(':o',"<img src="$img_dir/gasp.gif">");
  $strings&#1111;] = array(':\ ',"<img src="$img_dir/hm.gif"> ");
  $strings&#1111;] = array('._.',"<img src="$img_dir/beady.gif">");
  $strings&#1111;] = array('0:)',"<img src="$img_dir/angel.gif">");
  $strings&#1111;] = array(':x',"<img src="$img_dir/mouthshut.gif">");
  $strings&#1111;] = array('>.<',"<img src="$img_dir/yuck.gif">");
  $strings&#1111;] = array(':yawn:',"<img src="$img_dir/yawn.gif">");
  $strings&#1111;] = array('(@)',"<img src="$img_dir/hypnotized.gif">");
  $strings&#1111;] = array(':sword:',"<img src="$img_dir/sword.gif">");
  $strings&#1111;] = array(':thumbs:',"<img src="$img_dir/thumbsup.gif">");
  $strings&#1111;] = array(':yinyang:',"<img src="$img_dir/yinyang.gif">");
    foreach ($strings as $string) &#123;
	    $user_input = str_replace($string&#1111;0],$string&#1111;1],$user_input);
    &#125;
    echo $user_input;
&#125;

?>

Posted: Tue Jun 04, 2002 10:29 pm
by volka
or use regular expressions

Code: Select all

<?php
function escape_reg_chars(&$pattern)
&#123;
	$pattern = preg_replace( array('/\(/', '/\)/', '/\&#1111;/', '/\]/', '/\//', '/\./', '/\+/', '/\*/', '/\?/'),
													 array('\(', '\)', '\&#1111;', '\]', '\/', '\.', '\+', '\*', '\?'),
													 $pattern
												 );
&#125;

function embedPattern($patVal)
&#123;
	return ('/(^|\s)'.$patVal.'(\s|$)/');
&#125;

$img_dir = "http://www.neo-net.org/~test1/images/smileys"; 
$pattern&#1111;]=':)'; $replace&#1111;]="<img src="$img_dir/smile.gif">";
$pattern&#1111;]=':('; $replace&#1111;]="<img src="$img_dir/frown.gif">";
$pattern&#1111;]=';)'; $replace&#1111;]="<img src="$img_dir/wink.gif">";
$pattern&#1111;]=':P'; $replace&#1111;]="<img src="$img_dir/tongue.gif">";
$pattern&#1111;]=':D'; $replace&#1111;]="<img src="$img_dir/bigsmile.gif">";
$pattern&#1111;]=':&#1111;'; $replace&#1111;]="<img src="$img_dir/mad.gif">";
$pattern&#1111;]='^_^'; $replace&#1111;]="<img src="$img_dir/yay.gif">";
$pattern&#1111;]='-.-'; $replace&#1111;]="<img src="$img_dir/ugh.gif">";
$pattern&#1111;]='x_X'; $replace&#1111;]="<img src="$img_dir/xed.gif">";
$pattern&#1111;]='~_~'; $replace&#1111;]="<img src="$img_dir/bah.gif">";
$pattern&#1111;]=':*'; $replace&#1111;]="<img src="$img_dir/flirt.gif">";
$pattern&#1111;]=';('; $replace&#1111;]="<img src="$img_dir/sigh.gif">";
$pattern&#1111;]='-_-'; $replace&#1111;]="<img src="$img_dir/annoyed.gif">";
$pattern&#1111;]=':o'; $replace&#1111;]="<img src="$img_dir/gasp.gif">";
$pattern&#1111;]=':/'; $replace&#1111;]="<img src="$img_dir/hm.gif">";
$pattern&#1111;]='._.'; $replace&#1111;]="<img src="$img_dir/beady.gif">";
$pattern&#1111;]='0:'; $replace&#1111;]="<img src="$img_dir/angel.gif">";
$pattern&#1111;]=':x'; $replace&#1111;]="<img src="$img_dir/mouthshut.gif">";
$pattern&#1111;]='>.<'; $replace&#1111;]="<img src="$img_dir/yuck.gif">";
$pattern&#1111;]=':yawn:'; $replace&#1111;]="<img src="$img_dir/yawn.gif">";
$pattern&#1111;]='(@)'; $replace&#1111;]="<img src="$img_dir/hypnotized.gif">";
$pattern&#1111;]=':sword:'; $replace&#1111;]="<img src="$img_dir/sword.gif">";
$pattern&#1111;]=':thumbs:'; $replace&#1111;]="<img src="$img_dir/thumbsup.gif">";
$pattern&#1111;]=':yinyang:'; $replace&#1111;]="<img src="$img_dir/yinyang.gif">";
escape_reg_chars($pattern);
$pattern = array_map('embedPattern', $pattern);
$string = ':) test :thumbs: test :/';
$string = preg_replace($pattern, $replace, $string);
print($string);
?>
this also works if $string=':) text :)'
escape_reg_chars and embedPattern slows it down a bit, but filling $pattern is much easier ;)
I may have forgotten some special characters in escape_reg_chars.

Posted: Tue Jun 04, 2002 11:44 pm
by jason
volka: Except that in this case, RegEx is just overkill, and will would be much slower.

Posted: Wed Jun 05, 2002 12:11 am
by volka
yes it is - to be honest even worse than just simply 'slower' 8O
  • preg_replace: Did it in 0.22640800476074 seconds
  • str_replace: Did it in 0.015184044837952 seconds
using it's own source as input ten times concatenated (running seti@home in the background)
I thought it would be slower - but that much :?

But things change if the source is concatenated 200 times
  • preg_replace: Did it in 4.3865120410919 seconds
  • str_replace: Did it in 6.220116019249 seconds
but this test was unfair :twisted: since I kept the array in preg_replace but used the

Code: Select all

$string = str_replace($pattern&#1111;$i], $replace&#1111;$i], $string);
loop for str_replace

ok ok ;) only improvment is: it replaces smilies at the begin/end of a line properly :roll:

Posted: Wed Jun 05, 2002 8:27 am
by enygma
btw - str_replace can accept arrays too:

Code: Select all

$array1=array(":)",":(");
$array2=array("<img src="$img_dir/smile.gif">",,"<img src="$img_dir/frown.gif">");

$string=str_replace($array1,$array2,$string);

Posted: Wed Jun 05, 2002 10:59 am
by Wayne
try removing the white space around your smiley codes and change the quotations.
ie. ":)"
instead of ' :) '

code should look like
$string = str_replace(":)","<img src=\"$img_dir/smile.gif\">", $string);