3

I have a variable with more than 100 names, like this:

$var = "Josh" . ";" . "Chloe" . ";" . "Marie" . ";" . "John" . ";"...

And some texts that I want to do something if One name above is present in this texts:

$text1 = "this is an apple"; // do nothing, no name here.
$text2 = "marie is beautiful"; marie is in text2, so I want to call a function.

If one name in $var is in $text2 I want to call a function to do some actions like this:

if name found{
 function insert();
}

Some ideas how can I check if one name in $var is present in $text?

thank you friends!

RGS
  • 4,062
  • 4
  • 31
  • 67

6 Answers6

4

Store the names in an array, not a string:

$names = array('Josh', 'Chloe');
$text1 = 'marie is beautiful';

foreach ($names as $name) {
  if (stristr($text1, $name) !== false) {
     // name found. Call function here
  }
}

If you can't store the names in an array, explode that string:

$names = explode(';', $var);
navnav
  • 204
  • 2
  • 9
2

You can use preg_replace_callback. Here is an example of changing the strings with a callback:

<?php
$names_string = "Josh" . ";" . "Chloe" . ";" . "Marie" . ";" . "John";
$texts = array(
    "this is an apple", 
    "marie is beautiful"
);
$names_array = explode(';', $names_string); // Create an array of names
$pattern = '/' . implode('|', $names_array) . '/i';
var_dump(preg_replace_callback(
    $pattern, 
    function ($matches) { 
        return strtoupper($matches[0]);
    }, 
    $texts)
);

Output:

array (size=2)
  0 => string 'this is an apple' (length=16)
  1 => string 'MARIE is beautiful' (length=18)
Progrock
  • 7,373
  • 1
  • 19
  • 25
1

You can use something like this if the names MUST be in the format you've provided:

$var = "Josh" . ";" . "Chloe" . ";" . "Marie" . ";" . "John" . ";";

$regex = preg_quote($var);
$regex = str_replace(";","|",$regex);
$regex = preg_replace("/\|$/","",$regex);
$string = "marie is beautiful";

if (preg_match("/{$regex}/i",$string)) {
    echo "Run function for '{$string}'<br/>";
} else {
    echo "No match for '{$string}'<br/>";
}

$string = "this is an apple";
if (preg_match("/{$regex}/i",$string)) {
    echo "Run function for '{$string}'<br/>";
} else {
    echo "No match for '{$string}'<br/>";
}

This code generates this result:

Run function for 'marie is beautiful'
No match for 'this is an apple'
Michail Strokin
  • 531
  • 3
  • 8
1

You can split your string variable with names and then cycle the array using strpost to check if text contain every name.

function nameFound($names, $text){  
    $array_names=explode(';', $names);
    $bFound="not found";    
    foreach($array_names as $name){     
        $pos = strpos($text, $name);    
        if ($pos === false) {                                               
        }       
        else{
            $bFound="found!!!!";
        }
    }
    return $bFound;
}

$var = "Josh;Chloe;Marie;John";
$text1 = "this is an apple"; // do nothing, no name here.
$text2 = "Marie is beautiful"; //marie is in text2, so I want to call a function.
echo "Result for text 1: ".nameFound($var,$text1);
echo " --- Result for text 2: ".nameFound($var,$text2);   
dantella
  • 66
  • 4
1

If you need to use the string. You will have to split it into an array first using the explode method. However you will have to remove or drop the last ; in your string before using the explode method. If you do not you will get an empty delimiter error for your stristr or strstr function calls for the last index of the array.

<?php

    $var = "Josh" . ";" . "Chloe" . ";" . "Marie" . ";" . "John";

    //split $var into an array(Remove the last ; or you will get an error)
    //Warning: stristr() [function.stristr]: Empty delimiter in C:\wamp\www\toolbox\test.php on line 17
    $namesArray = explode(";",$var);


    $text2 = "marie is beautiful"; //marie is in text2, so I want to call a function.


    //loop through the names array
    foreach($namesArray as $name){      



    if(stristr($text2,$name) != false){


        doSomething();

    }

    }

    function doSomething(){

        echo "do something";

    }


    ?>

If you want a more reusable version then I would build a function something like the following:

     <?php

$var = "Josh" . ";" . "Chloe" . ";" . "Marie" . ";" . "John";

//split $var into an array(Remove the last ; or you will get an error)
//Warning: stristr() [function.stristr]: Empty delimiter in C:\wamp\www\toolbox\test.php on line 17
$namesArray = explode(";",$var);

//the input text to use as a test
$input = "marie is beautiful"; //marie is in text2, so I want to call a function.


//call the containsName function
containsName($input, $namesArray,"doSomething");


function containsName($input,$namesArray,$function){//begin function


//loop through the names array
foreach($namesArray as $name){      


//if the array index contains the name in the input variable
if(stristr($input,$name) != false){

    //call the function given by the $function parameter
    $function();

}

}

}//end function

function doSomething(){

    echo "do something";

}


?>
Larry Lane
  • 2,141
  • 1
  • 12
  • 18
0
  1. Store as array instead of string
  2. Follow the instructions here: Using an array as needles in strpos
Community
  • 1
  • 1
daxro
  • 193
  • 13