zoomzoom9 Posted January 27, 2014 Share Posted January 27, 2014 i'm creating code where i am trying to open a text file, read each line, check to see if each line is a palindrome or not and then create a new text file displaying the original line of text as well as saying if it is a palindrome or not....here is my code. what am i doing wrong? please help! <?php $out = fopen('results.txt', 'a'); foreach (file('testPhrases.txt') as $line) { $line = trim(str_replace('/[^a-zA-Z]+/', ' ', '', $line)); $reverse = strrev($line); if ($line == $reverse){ echo fwrite($out, $line . "This is a palindrome.\n"); } else{ echo fwrite($out, $line . "This is not a palindrome.\n"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285717-issue-with-outputting-a-text-fileplease-help/ Share on other sites More sharing options...
Ch0cu3r Posted January 27, 2014 Share Posted January 27, 2014 (edited) First str_replace does not take a regex pattern for the first (search) argument and the subject string should be the third argument. You should use preg_replace instead for regex $line = trim(preg_replace('/[^a-zA-Z]+/', ' ', $line)); Second you are echo'ing the result of fwrite, which is the number of bytes written. Not the text that was written. Change the if/else to $line .= " - This is " . (($line == $reverse) ? 'a' : 'not a') ." palindrome.\n"; // append this to current line echo "$line<br/>"; fwrite($out, $line); Edited January 27, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285717-issue-with-outputting-a-text-fileplease-help/#findComment-1466755 Share on other sites More sharing options...
zoomzoom9 Posted January 27, 2014 Author Share Posted January 27, 2014 thanks for the help. i see what i was doing wrong now so thanks for the explanation too it seems to be determining what is or is not a drome, but it is saying phrases aren't when they are. i think this is my line involving capital letters, punctuation and spaces. ('/[^a-zA-Z]+/', ' ', $line) did i not code that part right? Quote Link to comment https://forums.phpfreaks.com/topic/285717-issue-with-outputting-a-text-fileplease-help/#findComment-1466759 Share on other sites More sharing options...
Ch0cu3r Posted January 27, 2014 Share Posted January 27, 2014 (edited) /[^a-zA-Z]+/ is replacing any non letter character into a space, removing all punctuation within the line. Edited January 27, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285717-issue-with-outputting-a-text-fileplease-help/#findComment-1466760 Share on other sites More sharing options...
zoomzoom9 Posted January 27, 2014 Author Share Posted January 27, 2014 ok so then how would i remove the punctuation and also remove the space so that a palindrome like \\ No trace; not one carton \\ would be recognized as a palindrome Quote Link to comment https://forums.phpfreaks.com/topic/285717-issue-with-outputting-a-text-fileplease-help/#findComment-1466761 Share on other sites More sharing options...
.josh Posted January 27, 2014 Share Posted January 27, 2014 perhaps a matter of case-sensitivity, need to strtolower $line ? If that doesn't work, then post some example lines from your file. ok so then how would i remove the punctuation and also remove the space so that a palindrome like \\ No trace; not one carton \\ would be recognized as a palindrome replace with '' instead of ' ' so that all non-alpha chars are stripped. Quote Link to comment https://forums.phpfreaks.com/topic/285717-issue-with-outputting-a-text-fileplease-help/#findComment-1466763 Share on other sites More sharing options...
.josh Posted January 27, 2014 Share Posted January 27, 2014 $line = strtolower(trim(preg_replace('/[^a-z]/i', '', $line))); Quote Link to comment https://forums.phpfreaks.com/topic/285717-issue-with-outputting-a-text-fileplease-help/#findComment-1466764 Share on other sites More sharing options...
zoomzoom9 Posted January 27, 2014 Author Share Posted January 27, 2014 thats perfect! thank you so much for the help everyone Quote Link to comment https://forums.phpfreaks.com/topic/285717-issue-with-outputting-a-text-fileplease-help/#findComment-1466766 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.