3

I have 2 strings $verify and $test. I want to check if $test contains elements from $verify at specific points. $test[4] = e and $test[9] = ]. How can I check if e and ] in $verify?

$verify = "aes7]";
$test = "09ske-2?;]3fs{";

if ($test[4] == $verify AND $test[9] == $verify) {
    echo "Match";
} else {
    echo "No Match";
}
rzaratx
  • 756
  • 3
  • 9
  • 29

2 Answers2

4

Use strpos() https://www.php.net/manual/en/function.strpos.php

https://paiza.io/projects/n10TMV4Ate8-vtzioDrsMg

<?php

$verify = "aes7]";
$test = "09ske-2?;]3fs{";

if (strpos($verify, $test[4]) !== false && strpos($verify, $test[9]) !== false) {
    echo "Match";
} else {
    echo "No Match";
}
?>
symlink
  • 11,984
  • 7
  • 29
  • 50
2

As of PHP8 you can use str_contains()

<?php

$verify = "aes7]";
$test = "09ske-2?;]3fs{";

if (str_contains($verify, $test[4]) && str_contains($verify, $test[9])) {
    echo "Match";
} else {
    echo "No Match";
}
?>

Thanks to @symlink for the code snippet I butchered