Deepak
Deepak

Reputation: 45

Echo words not found in string comparison

I have got a variable and an array (can be a variable as well) which need to be compared. I know how to compare two strings, but I don't know how to output the words which haven't been found. Also the words which haven't been found need to be in a variable so that I can use them later on.

$mystring = array('Net 40', 'Net 44', 'Priv 40');
$findme   = 'Net 44';
if( strpos($mystring, $findme) !== false ) echo $findme." has been found in the string";

EDIT
I shall rephrase my question: How to compare two variables and output all words which haven't been found.

Upvotes: 0

Views: 67

Answers (5)

Progrock
Progrock

Reputation: 7485

Haven't been found is a little ambiguous, just be careful:

<?php

$haystack = array('Net40', 'Net44', 'Priv40');
$needles  = array('Net44', 'NetBsd');

var_dump(array_diff($haystack, $needles));
var_dump(array_diff($needles, $haystack));
var_dump(array_intersect($haystack, $needles));

Output:

array (size=2)
  0 => string 'Net40' (length=5)
  2 => string 'Priv40' (length=6)

array (size=1)
  1 => string 'NetBsd' (length=6)

array (size=1)
  1 => string 'Net44' (length=5)

Upvotes: 0

Piotr Siekierski
Piotr Siekierski

Reputation: 494

Solution with arrays. If your have strings, you can convert them to array with explode function.

 <?php
    $mystring = array('Net40', 'Net44', 'Priv40');
    $findme   = array('Net44');
    $result=array_diff($mystring,$findme);

    //$result is an array of elements that was in $mystring but not in $findme
    print_r($result);
    ?>

Upvotes: 0

trincot
trincot

Reputation: 350310

You could use array_diff to find the words that remain after you have successfully matched a word with one in a list:

$mystring = array('Net40', 'Net44', 'Priv40');
$findme   = 'Net44';

echo "<br>list of words before: " . implode(",", $mystring);

$mystring = array_diff($mystring, array($findme));

echo "<br>list of words after: " . implode(",", $mystring);

Output:

list of words before: Net40,Net44,Priv40
list of words after: Net40,Priv40

Upvotes: 1

JRsz
JRsz

Reputation: 2941

First, you should not use strpos with an array and a string.

You could make something like this:

$mystring = array('Net 40', 'Net 44', 'Priv 40');
$findme   = 'Net 44';

$size = count($mystring)
$notFound = array();

for($i = 0; $i < $size; $i++)
{
    if($mystring[$i] === $findme)
    {
         echo $findme." has been found in the string (actually array)";
    }
    else
    {
        array_push($notFound, $mystring[$i]);
    }
}

If mystring is actually supposed to be a string this code has to look a little bit different. I went with what you provided.

Upvotes: 0

Keyur Mistry
Keyur Mistry

Reputation: 926

strpos check that your second parameter is in the first parameter? e.g.

strpos("This is", "is")

this checks that 'is' is in the string or not. If you want to check the result is in array or not please do as follows

if(in_array($findme,mystring)) echo $findme." has been found in the string";

Upvotes: 0

Related Questions