1

I have this code:

$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
if(in_array("backup", $arr)){
    echo "Da";
} else { echo "Nu";
}

But is not working because,in_array instruction check the array for the complete string "backup" , which doesnt exist.I need to check for a part of the string,for example,to return true because backup is a part of the "Hello_backup" and "Beautiful_backup" strings

EDIT: I take the advice and i have used stripos like this:

$arr = array("Hello_backup-2014","World!","Beautiful_backup-2014","Day!");
$word='backup';
if(stripos($arr,$word) !== false){
echo "Da";
} else { echo "Nu";}

but now i get an error: "stripos() expects parameter 1 to be string, array given in if(stripos($arr,$word) !== false){"

2
  • 1
    Your error comes from the fact that you are trying to use an array as if it was a string. You need to use foreach on your array to get every string that this array contains. Take a better look at @Ghost example. Commented Sep 10, 2014 at 8:50
  • 3
    @Lght not necessarily, if you want to treat all array values as one, you can use implode to join the array elements as a string. See my answer Commented Sep 10, 2014 at 8:56

6 Answers 6

1

Use implode to basically concatenate the array values as a string, then use strpos to check for a string within a string.

The first argument you pass to implode is used to separate each value in the array.

$array = array("Hello_backup","World!","Beautiful_backup","Day!");
$r = implode(" ", $array);

if (strpos($r, "backup") !== false) {
    echo "found";
}
Sign up to request clarification or add additional context in comments.

Comments

1

In this case you need to use stripos(). Example:

$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
$needle = 'backup';

function check($haystack, $needle) {
    foreach($haystack as $word) {
        if(stripos($word, $needle) !== false) {
            return 'Da!'; // if found
        }
    }
    return 'Nu'; // if not found
}

var_dump(check($arr, $needle));

Without a function:

$arr = array("Hello_backup","World!","Beautiful_backup","Day!");

$found = false;
foreach($arr as $word) {
    if(stripos($word, 'backup') !== false) {
        $found = true;
        break;
    }
}

if($found) {
    echo 'Da!';
} else {
    echo 'Nu';
}

4 Comments

an example without the function please? i am a beginner and i dont want to use functions yet
i have an error stripos() expects parameter 1 to be string, array
@Memphistoles if you do not want a function, check my revision
@Memphistoles no you cannot use stripos directly, you must loop it first (each string) dont use the whole array, check my revision
1

Try with strpos()

$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
foreach($arr as $v){
  echo (strpos($v,"backup")!== false ? "Da" : "Nu");
}

output :- DaNuDaNu

Comments

1

Here is the one line solution for you.

$arr = array("Hello_backup-2014","World!","Beautiful_backup-2014","Day!");

$returned_a = array_map(function($u){ if(stripos($u,'backup') !== false) return  "Da"; else return "Nu";}, $arr);

You can use $returned_a with array as your answer.. Array ( [0] => Da [1] => Nu [2] => Da [3] => Nu )

Comments

1

Use this method. It is little bit simple to use.

$matches = preg_grep('/backup/', $arr); 
$keys    = array_keys($matches);     
print_r($matches);

Look this working example

According to your question

$matches = preg_grep('/backup/', $arr); 
$keys    = array_keys($matches);         
$matches = trim($matches);    
if($matches != '')
{echo "Da";
}else { echo "Nu";}

Comments

1
<?php
    $arr = array("Hello_backup","World!","Beautiful_backup","Day!");
    foreach($arr as $arr1) {
        if (strpos ($arr1,"backup")) {
            echo "Da";
        } else {
            echo "Nu";
        }
    }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.