开发者

PHP - how to search in both key and values for a string?

I'm trying to find a string in CSS that's been converted to an array. What I want to do is find a string within the key or value of array, and show the css block.

I tried for a few hours but cant make any progress.

Any suggestions?

Code is generated using this PHP css parser at http://pastebin.com/fstMwd3q

Example below is find css block with string "upload" and show each of the css blocks that has that. Another example is find all css that has inline-开发者_开发知识库block.

Array
(

    [.qq-upload-cancel] => Array
        (
            [font-size] => 11px
        )

    [.qq-upload-failed-text] => Array
        (
            [display] => none
        )

    [.qq-upload-fail .qq-upload-failed-text] => Array
        (
            [display] => inline
        )

    [span.iconmorehelp] => Array
        (
            [display] => inline-block
            [height] => 18px
            [width] => 18px
        )

    [a.iconmoreinfo] => Array
        (
            [height] => 18px
            [width] => 18px
            [display] => inline-block
            [margin-top] => 3px
            [margin-right] => 3px
        )
)

*Here's my code based on solution from willium below. If someone can make this simpler, please post!! *

foreach($array as $key=>$item) {
    global $needle;
    $found = false;
    $result1='';
    $result2='';

    $result1=$key;
    if(strpos($key, $needle)) {
        $found=true;
    }
    foreach($item as $key=>$value) {        
        $result2.= $key . ":";
        $result2.= $value .":\n";
        if(strpos($key, $needle) || strpos($value, $needle)) {
            $found=true;
        }

    }
    if($found) echo "<pre>" . $result1 . "\n{\n" . $result2 . "\n}\n\n </pre>";
}


you can loop through the array and parse for key value with a foreach loop.

foreach($array as $item) {
    foreach($item as $key=>$value) {
        echo $key;
        echo $value;
    }
}


The simplest way for you to achieve what you want is to add another method to your cssparser class.

/**
*    Returns an arrray of rule names containing
*    the text in $cssFrag
**/ 
function findByCss($cssFrag)
{
    $result = null;
    $cssFrag = strtolower($cssFrag);
    $css = $this->css;
    foreach($css as $selector => $rule){
        if(stripos($selector, $cssFrag)){
            $result[] = $selector;
        } else {
           foreach($rule as $key => $property){
               if(stripos($key, $cssFrag) || stripos($property, $cssFrag)){
                   $result[] = $selector;
               }
           }
        }
    }
    return $result;
}

Then you can do $rules = $cssparser->findByCss('inline');

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜