0

When I try to make a user defined function using str_replace it has no effect. Components of the same code work perfectly when not in a user defined function. The following shows a simple representation of my problem.

<?php
function replacelist($thetext) {
    $seachfor = array("crackers","soup");
    $replacewith = array("shapes","stew");
    $newtext = str_replace($searchfor, $replacewith, $thetext);
    return $newtext." - plus some test text";
    }
?>


<?php
$mytext="Animal crackers in my soup";

$newphrase = replacelist($mytext);

echo $newphrase;
?>

The above results in

Animal crackers in my soup - plus some test text

Any help gratefully received.

Thanks David

4
  • 4
    Activate error reporting! It'll tell you that you've typo'd your variable name. Commented Jul 9, 2014 at 11:33
  • TYPO - $seachfor and $searchfor Commented Jul 9, 2014 at 11:35
  • Generally it is a good idea to copy/paste your variables to avoid misspellings. Commented Jul 9, 2014 at 11:35
  • 1
    @Koninos Or use a decent IDE... Commented Jul 9, 2014 at 11:36

1 Answer 1

3

Spell $seachfor correctly and it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

That's embarassing. I have my more complex version working now. - thanks very much for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.