newsuper Posted August 13, 2008 Share Posted August 13, 2008 Hi, I'm having a small problem here.... In the code below, $file is a filename, something like "200.08.01 Google Buy.doc" What I want to do is search for either "Sell" or "Buy" in $file and display the cell and text in which the hyperlink appears accordingly (red background for "Sell" and blue for "Buy"). Also, if neither "Sell" or "Buy" appear in the filename, I want to display just a white background in the cell. The code sort of works in that I do get a red background when $file contains "Sell" and a blue one when it contains "Buy", but when $file contains neither values I get a blue background, instead of a white one. I'm sure it is pretty simple but it has me stumped! Is it because I am looking for (!strpos($file,"Sell")) which means "if it doesn't contain "Sell" then make the background blue? I have tried removing the ! from the strpos but that didn't work, everything went white! TIA if (!strpos($file,"Sell")) { ?><td bgcolor="blue"><font face="arial" size="0.7" color="white"><? echo "<a style='color: white; text-decoration: none; background: blue' href=file:///f:/research/$code/$file3>"; echo substr_replace($file ,"",-4); echo "</a></td>"; } elseif (!strpos($file,"Buy")){ substr_replace($file ,"",-1); ?><td bgcolor="red"><font face="arial" size="0.7" color="white"><? echo "<a style='color: black; text-decoration: none; background: red' href=file:///f:/research/$code/$file3>"; echo substr_replace($file ,"",-4); echo "</a></td>"; }else{ substr_replace($file ,"",-1); ?><td bgcolor="white"><font face="arial" size="0.7" color="black"><? echo "<a style='color: black; text-decoration: none; background: white' href=file:///f:/research/$code/$file3>"; echo substr_replace($file ,"",-4); echo "</a></td>"; } Link to comment https://forums.phpfreaks.com/topic/119410-solved-searching-for-a-string-in-a-variable-and-if-it-is-there/ Share on other sites More sharing options...
genericnumber1 Posted August 13, 2008 Share Posted August 13, 2008 Messy, hard to read code makes me angry <?php $bgColor = 'white'; $textColor = 'black'; if (strstr($file, "Sell") !== false) { // or stristr() if you want case insensitivity $bgColor = 'red'; $textColor = 'white'; } elseif (strstr($file, "Buy") !== false) { // see above $bgColor = 'blue'; $textColor = 'white'; } echo ' <td bgcolor="'.$bgColor.'"> <font face="arial" size="0.7" color="'.$textColor.'"> <a style="color: '.$textColor.'; text-decoration: none; background: '.$bgColor.'" href="file:///f:/research/'.$code.'/'.$file3.'"> '.substr_replace($file, "", -4).' </a> </font> </td> '; ?> Adjust the colors as you like, your colors were really mixed up in the original code so I just based it after what you said in the description. Link to comment https://forums.phpfreaks.com/topic/119410-solved-searching-for-a-string-in-a-variable-and-if-it-is-there/#findComment-615164 Share on other sites More sharing options...
newsuper Posted August 13, 2008 Author Share Posted August 13, 2008 That worked perfectly, thanks very much. Point taken about the messy confusing code - your example was wonderfully written. It's lucky I don't do this for a living! Link to comment https://forums.phpfreaks.com/topic/119410-solved-searching-for-a-string-in-a-variable-and-if-it-is-there/#findComment-615172 Share on other sites More sharing options...
genericnumber1 Posted August 13, 2008 Share Posted August 13, 2008 That worked perfectly, thanks very much. Point taken about the messy confusing code - your example was wonderfully written. It's lucky I don't do this for a living! Hah, neither do I... but you'll kick yourself if you come back to a script after a few months of not working on it and trying to figure out what you did I speak from experience. Link to comment https://forums.phpfreaks.com/topic/119410-solved-searching-for-a-string-in-a-variable-and-if-it-is-there/#findComment-615177 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.