Jump to content

Recommended Posts

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

 

 

 

 

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 }

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);
}


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..";

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";

 

 

 

 

 

 

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.

$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);
}

...
?>

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?

$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;
}

...
?>

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.

$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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.