devWhiz Posted June 9, 2011 Share Posted June 9, 2011 what I want to do is... $string = "Yellow, Green, Blue.."; if the string contains only 'Yellow,' it writes the string to only itsyellow.txt. if the string contains both 'Yellow,' and 'Blue..' it writes the string to itsyellow.txt and blue.txt. if the string contains only 'Green,' it writes the string to only itsgreen.txt. if the string contains both 'Green,' and 'Blue..' it writes the string to itsgreen.txt and blue.txt. the string can only contains either... 'Yellow,' Both 'Yellow,' and 'Blue..' 'Green,' Both 'Green,' and 'Blue..' I just put both Yellow, and Green, in the string as an example any little bit of help with this is appreciated Id like to be able to use file_put_contents() to keep the code short if possible Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/ Share on other sites More sharing options...
cyberRobot Posted June 9, 2011 Share Posted June 9, 2011 You could use something like strpos() to figure out if the string contains the various colors. http://php.net/manual/en/function.strpos.php Then just create some if statements to write to the files: if(contains yellow) { write to yellow.txt } if(contains green) { write to green.txt } if(contains blue) { write to blue.txt } Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227372 Share on other sites More sharing options...
devWhiz Posted June 9, 2011 Author Share Posted June 9, 2011 Hmm, I was using stristr() to check and see if the color was in the string.. I wrote this, what can I do to shorten up the code? if(stristr($string, 'Yellow,')) { file_put_contents('Yellow.txt', $string, FILE_APPEND); } if(stristr($string, 'Yellow,') && stristr($string, 'blue..')) { file_put_contents('Yellow.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); } if(stristr($string, 'Green,')) { file_put_contents('Green.txt', $string, FILE_APPEND); } if(stristr($string, 'Green,') && stristr($string, 'blue..')) { file_put_contents('Green.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); } Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227380 Share on other sites More sharing options...
devWhiz Posted June 9, 2011 Author Share Posted June 9, 2011 posted the wrong code Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227381 Share on other sites More sharing options...
cyberRobot Posted June 9, 2011 Share Posted June 9, 2011 Are those the only four tests? For example, will blue ever appear by itself? If it does, do you still want to print to blue.txt? Also, where does the string come from? Could the following happen: $string = "Blue, Green, Yellow.."; $string = "Yellow.."; Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227383 Share on other sites More sharing options...
devWhiz Posted June 9, 2011 Author Share Posted June 9, 2011 only posibilities are.. string contains ONLY yellow string contains BOTH yellow and blue string contains ONLY green string contains BOTH green and blue blue will never be by itself, green and yellow will never be together, it is together in my code only as an example.. so you could have $string = "yellow"; $string = "yellow, blue"; $string = "green"; $string = "green, blue"; Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227387 Share on other sites More sharing options...
cyberRobot Posted June 9, 2011 Share Posted June 9, 2011 With that being the case, I'm not sure about shortening the code. But you could modify it to limit the number of tests the code executes: <?php ... if(stristr($string, 'Yellow,') && stristr($string, 'blue..')) { file_put_contents('Yellow.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); } elseif(stristr($string, 'Yellow,')) { file_put_contents('Yellow.txt', $string, FILE_APPEND); } if(stristr($string, 'Green,') && stristr($string, 'blue..')) { file_put_contents('Green.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); } elseif(stristr($string, 'Green,')) { file_put_contents('Green.txt', $string, FILE_APPEND); } ... ?> This way if it contains both Yellow & Blue, the test to see if it's Yellow isn't executed. Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227391 Share on other sites More sharing options...
cyberRobot Posted June 9, 2011 Share Posted June 9, 2011 $string = "yellow"; $string = "yellow, blue"; $string = "green"; $string = "green, blue"; If those are the only four possibilities, you could do something like: <?php ... if($string == 'yellow')) { file_put_contents('Yellow.txt', $string, FILE_APPEND); } elseif($string == 'yellow, blue') { file_put_contents('Yellow.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); } elseif($string == 'green') { file_put_contents('Green.txt', $string, FILE_APPEND); } elseif($string == 'green, blue') { file_put_contents('Green.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); } ... ?> Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227394 Share on other sites More sharing options...
devWhiz Posted June 9, 2011 Author Share Posted June 9, 2011 this was the code I wrote before I made the thread, if(stristr($string, 'Yellow,')) { file_put_contents('Yellow.txt', $string, FILE_APPEND); } else if(stristr($string, 'Yellow,') && stristr($string, 'blue..')) { file_put_contents('Yellow.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); } else if(stristr($string, 'Green,')) { file_put_contents('Green.txt', $string, FILE_APPEND); } else if(stristr($string, 'Green,') && stristr($string, 'blue..')) { file_put_contents('Green.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); } its very similar to what you posted, yours works and writes to both files, mine only wrote to one file even in the string included 2 of the colors, any idea why? is it because I used else if instead of elseif? Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227396 Share on other sites More sharing options...
cyberRobot Posted June 9, 2011 Share Posted June 9, 2011 $string = "yellow"; $string = "yellow, blue"; $string = "green"; $string = "green, blue"; If those are the only four possibilities, you could do something like: <?php ... ?> Or maybe something a little cleaner: <?php ... switch($string) { case 'yellow': file_put_contents('Yellow.txt', $string, FILE_APPEND); break; case 'yellow, blue': file_put_contents('Yellow.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); break; case 'green': file_put_contents('Green.txt', $string, FILE_APPEND); break; case 'green, blue': file_put_contents('Green.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); break; } ... ?> Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227397 Share on other sites More sharing options...
cyberRobot Posted June 9, 2011 Share Posted June 9, 2011 its very similar to what you posted, yours works and writes to both files, mine only wrote to one file even in the string included 2 of the colors, any idea why? is it because I used else if instead of elseif? It's because you're testing "Yellow" before "Yellow, Blue". The problem is that "Yellow, Blue" contains "Yellow" so the first if always gets executed. Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227401 Share on other sites More sharing options...
devWhiz Posted June 9, 2011 Author Share Posted June 9, 2011 $string = "yellow"; $string = "yellow, blue"; $string = "green"; $string = "green, blue"; If those are the only four possibilities, you could do something like: <?php ... ?> Or maybe something a little cleaner: <?php ... switch($string) { case 'yellow': file_put_contents('Yellow.txt', $string, FILE_APPEND); break; case 'yellow, blue': file_put_contents('Yellow.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); break; case 'green': file_put_contents('Green.txt', $string, FILE_APPEND); break; case 'green, blue': file_put_contents('Green.txt', $string, FILE_APPEND); file_put_contents('blue.txt', $string, FILE_APPEND); break; } ... ?> hmm, problem with that is.. the string could sometimes look like $string = "Yellow, a bunch of other text, blue.. , more text, etc" the other text in the string varies Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227404 Share on other sites More sharing options...
cyberRobot Posted June 9, 2011 Share Posted June 9, 2011 hmm, problem with that is.. the string could sometimes look like $string = "Yellow, a bunch of other text, blue.. , more text, etc" the other text in the string varies Yep, that would make solutions from Reply 7 & 9 unusable. Since I went a little crazy with responses, I wanted to make sure you saw my response below: its very similar to what you posted, yours works and writes to both files, mine only wrote to one file even in the string included 2 of the colors, any idea why? is it because I used else if instead of elseif? It's because you're testing "Yellow" before "Yellow, Blue". The problem is that "Yellow, Blue" contains "Yellow" so the first if always gets executed. Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227411 Share on other sites More sharing options...
devWhiz Posted June 9, 2011 Author Share Posted June 9, 2011 ahh, That makes sense.. Thank you! I shot you a PM also Quote Link to comment https://forums.phpfreaks.com/topic/238868-write-string-to-2-files-at-the-same-time-if-statement-is-true/#findComment-1227423 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.