0

str_replace doesn't seem to be working as we expect it to.

We have a text file and we're trying to remove part of the file.

while(!feof($bodyfile)) {

    $content = @fgets($bodyfile);

    $content = str_replace("MARGIN","",$content);

(Obviously fopen is used to open the file as 'r') Strangely enough, finding and replacing M works? but not margin..

5
  • 2
    What does $content look like ?? Commented Oct 9, 2012 at 9:05
  • str_replace is case-sensitive!str_ireplace is not! Commented Oct 9, 2012 at 9:08
  • yes we are trying to find the uppercase MARGIN and replace it with nothing... Commented Oct 9, 2012 at 9:10
  • $content is a text file that can be handled line by line. why has someone voted this question down? Commented Oct 9, 2012 at 9:10
  • can you add .. the content to pastebin .. i would like to test myself Commented Oct 9, 2012 at 9:17

3 Answers 3

4

UPDATE:

fgets() function reads only 1 line at the time, and by putting that line in your $content variable, you're overwriting the replacement for previous line, and doing it over and over again.

Try with this:

$content = "";
while(!feof($bodyfile)) {

    $line = @fgets($bodyfile);

    $content .= str_replace("MARGIN","",$line);

So, what this code does is reading the line and assigning it to the $line variable, and then adding the replaced string to your $content variable.


By adding @ sign in front of your functions, you're suppressing errors which that function gives.
Try to remove @ from your @fgets and see if there's any error.

Try var_dump($content) or echo $content to see if file is loaded correctly.

Remember that str_replace() is case sensitive.

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

4 Comments

var_dump shows the whole thing is a bunch of strings. which is what we want. removing the @ didn't do anything as there were no errors to suppress. the file looks to be loading fine. the file is basically a line by line text document of data that has html in. May I point out that also, we can str_replace ONE character but when choosing multiple is doesnt work... I thought this was because the file was being read character by character but i dont think that is the case after some investigation with line breaks
@Nikola K. reading one line is valid and they can be so many text in a line .... it is fgetc that many not be valid in this case ...
Sorry - that one just loops forever :(. could this be a character encoding issue?
@30secondstosam that is why i asked you to add the content to pastbin .. understand what we are dealing with
0

you could do:

$str=implode("",file('somefile.txt'));
$fp=fopen('somefile.txt','w');
$str=str_replace('MARGIN','',$str);
//OR
//$str=str_ireplace('MARGIN','',$str); for case insensitivity
fwrite($fp,$str,strlen($str));

Comments

0

Just found out the file is a UTF-16 character encoding rather than UTF-8 for some obscure reason. Converted, now my method originally works!

Thanks to all for suggestions

Comments

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.