Regex Expression在在线测试人员中运行良好,但在我的页面上无效

时间:2014-07-06 20:36:21

标签: php css regex

所以我正在阅读一个css文件,我想从中获取一些数据。我目前正在将数据简化为:

font-family: "Verdana";
font-size: 14px;
color: #000000;

并且纯粹得到我需要的数据(即Verdana,14和#000000),我创建了以下PHP代码:

foreach($tmp as $tmpstr){
        if(strpos($tmpstr, "font-family")){
            $tmpres = array();
            preg_match('/\"(.*?)\"/', $tmpstr, $tmpres);
            $returnData["website"]["font-family"] = $tmpres[1];
        } else if(strpos($tmpstr, "font-size")){
            $tmpres2 = array();
            preg_match_all('/\:\s(.*?)px\;/', $tmpstr, $tmpres2);
            $returnData["website"]["font-size"] = $tmpres2[1];
        } else if(strpos($tmpstr, "color")){
            $tmpres3 = array();
            preg_match_all('/color\:\s(.*?)\;/', $tmpstr, $tmpres3);
            $returnData["website"]["font-color"] = $tmpres3[1];
        }
    }

我知道它还没有优化,但原因是,它不起作用。获得Verdana的第一个表达式'/\"(.*?)\"/'可以正常工作,但另外两个('/\:\s(.*?)px\;/'获取字体大小,/color\:\s(.*?)\;/来获取字体颜色)不是,而是测试它们在线测试仪似乎确实有效。谁能告诉我我做错了什么?

谢谢!

2 个答案:

答案 0 :(得分:1)

检查此示例

$tmp = array(
    'font-family: "Verdana";',
    'font-size: 14px;',
    'color: #000000;'
);

foreach($tmp as $tmpstr)
{
    if (false !== strpos($tmpstr, "font-family"))
    {
        preg_match('/:[ ]*"(.*)"/', $tmpstr, $tmpres);
        echo $tmpres[1];
    } 
    else if (false !== strpos($tmpstr, "font-size"))
    {
        preg_match('/:[ ]*(\d*)px/', $tmpstr, $tmpres2);
        echo $tmpres2[1];
    } 
    else if (false !== strpos($tmpstr, "color"))
    {
        preg_match('/:[ ]*(.*);/', $tmpstr, $tmpres3);
        echo $tmpres3[1];
    }

    echo '<br />';
}

结果:

Verdana
14
#000000

希望此解决方案有所帮助!

答案 1 :(得分:0)

试试这个:

$css = 'font-family: "Verdana";
font-size: 14px;
color: #000000;'


preg_match_all('(?P<key>.+?)\:\s*(?P<value>.+?)\;', $css, $result, PREG_SET_ORDER);

输出:

Array
(
    [0] => Array
        (
            [0] => font-family: "Verdana";
            [key] => font-family
            [1] => font-family
            [value] => "Verdana"
            [2] => "Verdana"
        )

    [1] => Array
        (
            [0] => font-size: 14px;
            [key] => font-size
            [1] => font-size
            [value] => 14px
            [2] => 14px
        )

    [2] => Array
        (
            [0] => color: #000000;
            [key] => color
            [1] => color
            [value] => #000000
            [2] => #000000
        )

)

然后,做一个foreach循环来清理输出应该是一件小事:

$data = array()
foreach($result as $r){
  $data[$r['key']] = $r['value'];
}

输出:

array
(
     [font-family] => "Verdana",
     [font-size] => 14px,
     [color] => #000000
)

作为旁注,您可以为整个文件扩展它:

/(?P<selectors>.*?)\s*\{|(?P<key>.+?)\:\s*(?P<value>.+?)\;/



 #id, and some class
    {
    font-family: "Verdana";
    font-size: 14px;
    color: #000000;
    }

   #id2, and some class1
    { ..... and so on.

输出:

array
(
    [0] => Array
        (
            [0] => #id, and some class
{
            [selectors] => #id, and some class ///you may want to post process this and split on comma etc.
            [1] => #id, and some class
        )



       [1] => Array
            (
                [0] => font-family: "Verdana";
                [selectors] => 
                [1] => 
                [key] => font-family
                [2] => font-family
                [value] => "Verdana"
                [3] => "Verdana"
            )

        [2] => Array
            (
                [0] => font-size: 14px;
                [selectors] => 
                [1] => 
                [key] => font-size
                [2] => font-size
                [value] => 14px
                [3] => 14px
            )

        [3] => Array
            (
                [0] => color: #000000;
                [selectors] => 
                [1] => 
                [key] => color
                [2] => color
                [value] => #000000
                [3] => #000000
             )
         [4] => Array
            (
                [0] => #id2, and some class1
    {
                [selectors] => #id2, and some class1
                [1] => #id2, and some class1
            ) .....

更新,抱歉,您想要检查是否为空(之前我已经设置了),因为我看到每个都设置了$ r [&#39;选择器&#39;]。然后,您可以通过其选择器对每个样式块进行子数组,并解析您的哈特内容。你明白了,但是如果你需要帮助,那就问一下(在这种情况下删除PRET_SET_ORDER可能更好)...

这将是我建议的css文件的最终结构..

array(
selector => array( color => 'bla', font => blabla )
selector2 => array( ..... 
)

我认为preg_match_all并没有得到它所需要的关注,它对这类事情非常有用,如果您不熟悉它,请在php doc http://www.php.net/manual/en/function.preg-match-all.php

上查看