Jump to content

[SOLVED] Searching for a string in a variable and if it is there.....


newsuper

Recommended Posts

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


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.

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 :D I speak from experience.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.