SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems with str_replace

    Hello people!

    I'm having problems using the str_replace command.

    I want to use two files. One contains which words to replace, and the other contains which words to replace them with.

    My script looks like this:


    $file = file_get_contents('example.html');
    $originalwords = "originalwords.txt";
    $newwords = "newwords.txt";

    $result = str_replace($originalwords, $newwords, $file);

    echo $result;



    For some reason $file isn't being affected by my str_replace command.

    Any ideas where I'm going wrong?

    Cheers.
    Lee.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Well sure...you are not pulling in the contents of the two files according to the code you posted...unless you are trying to replace the file names contained within the source...
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So how would I pull the contents into two arrays to use in the str_replace command?

    I've tried:

    $file1 = file_get_contents('originalwords.txt');
    $originalwords = explode("\n",$file1);

    $file2 = file_get_contents('newwords.txt');
    $newwords = explode("\n",$file2);


    but this didn't seem to work.

  4. #4
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically, I want to do a str_replace using two text files instead of arrays, but it isn't working..

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,819
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Try this:

    PHP Code:

    //  orriginalwords.txt
    tracy
    diana
    helen

    // newords.txt
    tom
    dick
    harry

    // example.html
    tracy
    diana
    helen

    <?php

    function fred($str)
    {
      echo 
    '<pre>'print_r($str); echo '</pre>';
    }



           
    $file1 file_get_contents('originalwords.txt');
            
    $originalwords explode("\n",$file1); 

            
    $file2 file_get_contents('newwords.txt');
            
    $newwords explode("\n",$file2);

            
    $file file_get_contents('example.html');
            echo 
    'before: <br />';
            
    fred($file);

            
    $result str_replace($originalwords$newwords$file);
            echo 
    'after:  <br />';
            
    fred($result);
    ?>

    // output
    before:
    tracy
    diana
    helen

    after:
    tom 
    dick
    harry
    I have tried this locally, copy the files, etc and once working then replace with your content and spot the differences.
    Last edited by John_Betong; Sep 1, 2012 at 17:22. Reason: spelling: not my fortay


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •