Syntax for strpos for true and false

Hi

I am a copy and paste coder, so missing a lot of basics.

I have a folder with a number of files eg
img-3-1-paint-city50
img-3-1-paint-city30
img-3-1-primer-city50
img-3-1-tape-city50

The code below sucessfully finds the first file (includes “paint” and “city50” and ignores all other files.

I know the code is checking if both “paint” and “city50” are true but the use of false is confusing me.

I want to change the code so that “paint” is not true but “city50” is true - so the following files will be returned:
img-3-1-primer-city50
img-3-1-tape-city50

What do I need to change so “paint” is false / not true

EDIT - jump to post 6 in this thread 31 December where I added my revised code and what I outcome I want. Thanks for the replies thus far.

if($file !== ‘.’ && $file !== ‘…’ && ( strpos($file,“paint”) !== false && strpos($file,“city50”) !== false ))

The reason for the comparison with a false value is covered in the documentation. Short-version: the position, if found, can be zero, which is also seen as a false value using a loose comparison/boolean, so, an exact comparison should be used (for a search which will never match anything starting at position zero, a loose/boolean comparison can be used.)

If strpos() (which is case-sensitive, use stripos() for a case-insensitive search) doesn’t find a value, the result will be a false. This would use strpos(…) === false. If strpos() does find a value, the result will be not false. This would use strpos(…) !== false.

To make writing the logic clearer (eliminate the negative logic), use a helper function -

// test if $string has/contains the $search value
function has($string,$search)
{
	return strpos($string,$search) !== false;
}

Next, any search that includes a specific value, doesn’t need to include the ‘.’ and ‘..’ tests.

Using the above has() function, your original test would look like -

if(has($file,"paint") && has($file,"city50"))

You new test would be -

if(!has($file,"paint") && has($file,"city50"))

Thanks for your prompt response. I am a little wiser but not enough to make this work.

I was aware that there is not jsut one false.

I am not sure if I am to do something with the code blow //test if…
but cannot figure out how to implement this.

My full code is bwlow if you or anyone else can give more help.

<?php
$Count1image2 = 0;
$image2;
$handle = opendir(dirname(realpath(__FILE__)).'/images/intro');
while($file = readdir($handle)){
	if($file !== '.' && $file !== '..' && ( strpos($file,"damage") !== false && strpos($file,"city50") !== false )){
		$image2[$Count1image2] = $file;
		$Count1image2++;
	}
}
sort($image2);
for($i=0; $i<$Count1image2; $i++)
	echo '<amp-img src="'.$img2_folder.$image2[$i].'" alt="'.$img2_alt.'" class="xs-12" width="353" height="210" layout="responsive"></amp-img>';
?>
$handle = opendir(dirname(realpath(__FILE__)).'/images/intro');
while($file = readdir($handle)){
	if($file !== '.' && $file !== '..' && 
( strpos(strlower($file),"paint") == -1 && 
strpos(strlower($file),"city50") !== false )){
		$image2[] = $file;
	}
}

Thanks for the code @astonecipher
I assume that I was to replace all mycode between and including
$handle…
and
}
}

Unfortunately that does not display image2 or any of the page below image 2.

I copy my full revised code below.

I was concerned that the end part of the counter code was not above your closing } }

		$image2[$Count1image2] = $file;
		$Count1image2++;

And have tried including and excluding these two lines in different locations - but I still get the same result.

		<?php
		$Count1image2 = 0;
		$image2;
			$handle = opendir(dirname(realpath(__FILE__)).'/images/intro');
			while($file = readdir($handle)){
				if($file !== '.' && $file !== '..' && 
			( strpos(strlower($file),"paint") == -1 && 
			strpos(strlower($file),"city50") !== false )){
			$image2[] = $file;
		}
	}
		$image2[$Count1image2] = $file;
		$Count1image2++;
		sort($image2);

for($i=0; $i<$Count1image2; $i++)
	echo '<amp-img src="'.$img2_folder.$image2[$i].'" alt="'.$img2_alt.'" class="xs-12" width="353" height="210" layout="responsive"></amp-img>';
						
			?>

Thanks for the contributions so far. Still keen to get an answer to this.

My current working code is copied below

It correctly finds images WHERE filename includes string “paint” and variable “$city

I want to change it so it finds all files in the same folder WHERE finename <> “paint” but does inlcude “$city

<?php $Count1image2 = 0; $image2; $handle = opendir(dirname(realpath(__FILE__)).'/'."$img2_folder"); while($file = readdir($handle)){ if($file !== '.' && $file !== '..' && ( strpos($file,"paint") !== false && strpos($file,"$city") !== false )){ $image2[$Count1image2] = $file; $Count1image2++; } } sort($image2); for($i=0; $i<$Count1image2; $i++) echo ''; ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service