Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have following set of an array

array('www.example.com/','www.example.com','www.demo.example.com/','www.example.com/blog','www.demo.com');

and I would like to get all element which matching following patterns,

$matchArray  = array('*.example.com/*','www.demo.com');

Expected result as

array('www.example.com/','www.demo.example.com/','www.example.com/blog','www.demo.com');

Thanks :)

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

This works:

    $valuesArray = array('www.example.com/','www.example.com','www.demo.example.com/','www.example.com/blog','www.demo.com');
    $matchArray  = array('*.example.com/*','www.demo.com');
    $matchesArray = array();

    foreach ($valuesArray as $value) {
        foreach ($matchArray as $match) {

            //These fix the pseudo regex match array values
            $match = preg_quote($match);
            $match = str_replace('\*', '.*', $match);
            $match = str_replace('/', '\/', $match);

            //Match and add to $matchesArray if not already found
            if (preg_match('/'.$match.'/', $value)) {
                if (!in_array($value, $matchesArray)) {
                    $matchesArray[] = $value;
                }
            }

        }
    }

    print_r($matchesArray);

But I would reccomend changing the syntax of your matches array to be actual regex patterns so that the fix section of code is not required.

share|improve this answer
 
thank you so much :) –  abhis Mar 6 '12 at 9:49
add comment
/\w+(\.demo)?\.example\.com\/\w*|www\.demo\.com/

regexr link

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.