Jump to content

Archived

This topic is now archived and is closed to further replies.

Shadow Hatake

str_replace()

Recommended Posts

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:

[code]<?php

$test = "a b";

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

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

?>[/code]

So how should I go about doing this?

Share this post


Link to post
Share on other sites
Just put echo or print in front of the str_replace function. That will echo the result.

Share this post


Link to post
Share on other sites
You could also put it on a function, so it will be easier to use.
[code]<?php

function replacewords($text) {

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

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

echo replacewords("a b");

?>[/code]

Share this post


Link to post
Share on other sites
[quote author=Shadow Hatake link=topic=99792.msg393177#msg393177 date=1152289934]
Daniel0: If you read my post I tried that. It sent it into an infinite loops.
[/quote]

Strange, worked perfectly for me.

Share this post


Link to post
Share on other sites
Odd. Would it make a difference if I was using more letters?

This is the whole code I'm using.
[code]
<?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);

?>[/code]

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

Share this post


Link to post
Share on other sites
<?

//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;

?>

Share this post


Link to post
Share on other sites
<?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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.