-3

we have an string like :

$string="Lorem Ipsum is simply dummy text of the printing and typesetting industry.";

so i want to check if some words are available in that string. for example the words :

$words=['Ipsum','54'];

and you can see Ipsum is in the string but 54 is not in string, so the function should call back true (because Ipsum found). I'm using php, thanks for helping.

Philipp
  • 15,377
  • 4
  • 35
  • 52
Ali YQB
  • 73
  • 1
  • 2
  • 8

2 Answers2

1

A simple loop combined with strpos should be all you need to achieve this

function containsOneOfThoseWords($str, $words) {
    foreach ($words as $word) {
        if (strpos($str, $word) !== false) return true;
    }
    return false;
}
Philipp
  • 15,377
  • 4
  • 35
  • 52
0
php > $string="Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
php > $lookfor = "simply";
php > echo strpos($string, $lookfor) > 0 ? true : false ;
1
php > $lookfor = "simplest";
php > echo strpos($string, $lookfor) > 0 ? true : false ;
//nothing will be printed -))

just make a function from above snippet.

update

$ cat search.php
<?php

$domain="asdasd asd fasdfasfafafad f454   asdfa";
$lookfor=array('as','f454');

function containsOneOfThoseWords($str, $words) {
    foreach ($words as $word) {
        if (strpos($str, $word) !== false) return true;
    }
    return false;
}

echo containsOneOfThoseWords($domain, $lookfor);
?>
$ php search.php; echo 
1
marmeladze
  • 6,468
  • 3
  • 24
  • 45