Jump to content


Photo

str_replace()


  • Please log in to reply
7 replies to this topic

#1 Shadow Hatake

Shadow Hatake
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 07 July 2006 - 03:46 PM

Okay I have a problem. I want to echo the results of a str_replace() but when I put echo infront of it it goes into a huge loop. Here's my code:

<?php

$test = "a b";

$letters = array('a', 'b');
$replace = array('t455', '5gr4');

str_replace($letters, $replace, $test);

?>

So how should I go about doing this?

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 07 July 2006 - 03:55 PM

Just put echo or print in front of the str_replace function. That will echo the result.

#3 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 07 July 2006 - 04:09 PM

You could also put it on a function, so it will be easier to use.
<?php

function replacewords($text) {

$letters = array('a', 'b');
$replace = array('t455', '5gr4');

$text = str_replace($letters, $replace, $text);
return $text;
}

echo replacewords("a b");

?>


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#4 Shadow Hatake

Shadow Hatake
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 07 July 2006 - 04:32 PM

Daniel0: If you read my post I tried that. It sent it into an infinite loops.

hackerkts: I'll try that.

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 07 July 2006 - 05:08 PM

Daniel0: If you read my post I tried that. It sent it into an infinite loops.


Strange, worked perfectly for me.

#6 Shadow Hatake

Shadow Hatake
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 07 July 2006 - 08:31 PM

Odd. Would it make a difference if I was using more letters?

This is the whole code I'm using.
<?php

$test = "a";

$letters = array( 'a', 
				  'b', 
				  'c', 
				  'd', 
				  'e', 
				  'f',
				  'g',
				  'h',
				  'i',
				  'j',
				  'k',
				  'l',
				  'm',
				  'n',
				  'o',
				  'p',
				  'q',
				  'r',
				  's',
				  't',
				  'u',
				  'v',
				  'w',
				  'x',
				  'y',
				  'z');
				  
$replace = array( 'b5fc',
				  'c8hd',
			      'd8re',
			      'ej9f',
			      'f8jg',
			      'g8ph',
			      'hd7i',
			      'iokj',
			      'ji8k',
			      'kh0l',
			      'lbhm',
			      'm87n',
			      'nuvo',
			      'ohvp',
			      'p7yq',
			      'qjhr',
			      'ribs',
			      'soit',
			      't9nu',
			      'ud6v',
			      'v89w',
			      'w9sx',
			      'x8sy',
			      'y9dz',
			      'z8ha',
			      'a9jb');

echo str_replace($letters, $replace, $test);

?>

http://www.infected-...com/replace.php

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 07 July 2006 - 09:27 PM

<?

//This is erigi replace with no array.

$a="i dont wont this text";

$b ="please print me!";

$replace=eregi_replace($a,"",$b);

echo $replace;

?>

<?

//This is eregi with array

$a=array("i dont wont this text");

$b=array("please print me!");

$replace=eregi_replace($a[0],"",$b[0]);

echo $replace;

?>


<?

//This is str_replace with no array.

$a="i dont wont this text";

$b ="please print me!";

$replace=str_replace($a,"",$b);

echo $replace;

?>

<?

//This is str_replace with array

$a=array("i dont wont this text");

$b=array("please print me!");

$replace=str_replace($a[0],"",$b[0]);

echo $replace;

?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 07 July 2006 - 09:39 PM

<?php



$letters = array( 'a',
  'b',
  'c',
  'd',
  'e',
  'f',
  'g',
  'h',
  'i',
  'j',
  'k',
  'l',
  'm',
  'n',
  'o',
  'p',
  'q',
  'r',
  's',
  't',
  'u',
  'v',
  'w',
  'x',
  'y',
  'z');
 
$replace = array( 'b5fc',
  'm8hd',
      'd8re',
      'ej9f',
      'f8jg',
      'g8ph',
      'hd7i',
      'iokj',
      'ji8k',
      'kh0l',
      'lbhm',
      'm87n',
      'nuvo',
      'ohvp',
      'p7yq',
      'qjhr',
      'ribs',
      'soit',
      't9nu',
      'ud6v',
      'v89w',
      'w9sx',
      'x8sy',
      'y9dz',
      'z8ha',
      'a9jb');


echo str_replace($letters[2]," ", $replace[1]);


hope this helps ok.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users