0

I have an array like:

arr = array("*" , "$" , "and" , "or" , "!" ,"/");

and another string like :

string = "this * is very beautiful but $ is more important in life.";

I'm looking the most efficient way with the lowest cost to find the member of the array in this string. Also I need to have an array in result that can show which members exist in the string.

The easiest way is using a for loop but I believe there should be more efficient ways to do this in PHP.

1
  • this looks like a template implementation, you could just use str_replace with arrays for the needle and replacement parameters Commented Jul 5, 2013 at 4:54

3 Answers 3

3
$arr=array("*" , "$" , "#" , "!");

$r = '~[' . preg_quote(implode('', $arr)) . ']~';

$str = "this * is very beautiful but $ is more important in life.";

preg_match_all($r, $str, $matches);

echo 'The following chars were found: ' . implode(', ', $matches[0]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, But It's not working if the array members are more than 1 char for example : "or" , "and"
@Amir Sa: generate the regex as (and|or|*|$) etc then
0

If you are looking for the most efficient way, the result of the following code is:

preg:1.03257489204

array_intersect:2.62625193596

strpos:0.814728021622

It looks like looping the array and matching using strpos is the most efficient way.

        $arr=array("*" , "$" , "#" , "!");

        $string="this * is very beautiful but $ is more important in life.";



        $time = microtime(true);

        for ($i=0; $i<100000; $i++){

            $r = '~[' . preg_quote(implode('', $arr)) . ']~';

            $str = "this * is very beautiful but $ is more important in life.";

            preg_match_all($r, $str, $matches);

        }

        echo "preg:". (microtime(true)-$time)."\n";

        $time = microtime(true);

        for ($i=0; $i<100000; $i++){

            $str = str_split($string);

            $out = array_intersect($arr, $str);

        }


        echo "array_intersect:". (microtime(true)-$time)."\n";


        $time = microtime(true);

        for ($i=0; $i<100000; $i++){

            $res = array();
            foreach($arr as $a){
                if(strpos($string, $a) !== false){
                    $res[] = $a;
                }
            }

        }

        echo "strpos:". (microtime(true)-$time)."\n";

Comments

0

You can use array_insersect

$string = "this * is very beautiful but $ is more important in life.";
$arr=array("*" , "$" , "#" , "!");

$str = str_split($string);

$out = array_intersect($arr, $str);

print_r($out);

This code will produce the following output

Array ( [0] => * [1] => $ )

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.