Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Regex表达式在在线测试人员中运行良好,但在我的页面上不起作用_Php_Css_Regex - Fatal编程技术网

Php Regex表达式在在线测试人员中运行良好,但在我的页面上不起作用

Php Regex表达式在在线测试人员中运行良好,但在我的页面上不起作用,php,css,regex,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();

所以我正在读一个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(.*?)/
获取字体颜色)工作正常,而在在线测试仪中测试它们似乎确实有效。谁能告诉我我做错了什么

谢谢

检查这个例子

$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 />';
}
希望这个解决方案有帮助

试试这个:

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


preg_match_all('(?P<key>.+?)\:\s*(?P<value>.+?)\;', $css, $result, PREG_SET_ORDER);
然后,执行foreach循环来清理输出应该是一件小事:

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

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
        )

)
array
(
     [font-family] => "Verdana",
     [font-size] => 14px,
     [color] => #000000
)
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
            ) .....
作为补充说明,您可以对整个文件进行扩展:

/(?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.
更新,很抱歉,您需要检查是否为空(以前我设置了isset),因为我看到每个选项中都设置了$r['selectors']。然后,您可以通过选择器对每个样式块进行子数组,并解析为hart的内容。你明白了,但是如果你需要这方面的帮助,那就问问吧(在这种情况下,最好删除PRET_SET_顺序)

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

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

我认为PrggMatMatyALL并没有得到它需要的注意,它对这类事情可能非常有用,如果你不熟悉它,在PHP DOC

检查它,其他2不运行,因为IF \ ELE结构,考虑你的逻辑第一个工作,它可能不是问题,但是注意,<代码>可以返回
0
,因此您需要将其与
false
进行比较,例如:
如果(strpos($tmpstr,“font size”)!==false)
使用preg match all,它是一个仅包含css样式的文件吗?您可以使用preg match all将整个文件提取到一个数组中,然后在可用数组结构中进行所需的处理。我会用这种方式将您的\s*(0或更多)更新为\s*(0或更多)其完全有效的css(字体系列:“Verdana”,字体系列:“Verdana”array( selector => array( color => 'bla', font => blabla ) selector2 => array( ..... )