简体   繁体   English

正则表达式在在线测试人员中工作正常,但在我的页面上不起作用

[英]Regex Expression works fine in online testers, but not working on my page

So I am reading a css file, and I want to get some data out of it. 所以我正在读取一个css文件,我想从中获取一些数据。 I currently trimmed down the data purely to this: 我目前仅将数据缩减为:

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

and to purely get the data I need (namely Verdana, 14 and #000000), I created the following PHP code: 为了纯粹获取我需要的数据(即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];
        }
    }

I know it is not optimized yet, but the reason for that is, is that it isn't working. 我知道它尚未进行优化,但原因是它无法正常工作。 The first expression '/\\"(.*?)\\"/' to get Verdana works fine, but the other two ( '/\\:\\s(.*?)px\\;/' to get the font size, /color\\:\\s(.*?)\\;/ to get the font color) don't, whereas testing them in an online tester does seem to work. 获得Verdana的第一个表达式'/\\"(.*?)\\"/'可以正常工作,而其他两个'/\\:\\s(.*?)px\\;/''/\\:\\s(.*?)px\\;/'获得字体大小, /color\\:\\s(.*?)\\;/来获取字体颜色),而在在线测试仪中进行测试似乎确实可行。 Can anyone tell me what I am doing wrong? 谁能告诉我我在做什么错?

Thanks! 谢谢!

Check this example 检查这个例子

$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 />';
}

Result: 结果:

Verdana
14
#000000

Hope this solution help! 希望此解决方案有帮助!

Try this: 尝试这个:

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


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

Outputs: 输出:

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
        )

)

Then it should be a trivial matter of doing a foreach loop to clean up the output: 然后,做一个foreach循环来清理输出应该是一件小事:

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

Outputs: 输出:

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

As a side note you could extend this for the whole file: 附带说明一下,您可以将其扩展到整个文件:

/(?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.

Outputs: 输出:

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
            ) .....

update, sorry you'll want to check if empty ( previously i had isset ) as I see that $r['selectors'] is set in each of them. 更新,对不起,您将要检查是否为空(以前我有isset),因为我看到每个变量中都设置了$ r ['selectors']。 Then you can sub array each style block by its selector, and parse to your hart's content. 然后,您可以通过其样式选择器对每个样式块进行子数组化,并解析为您的hart内容。 You get the idea, but if you need help with that bit just ask ( it may be better to remove the PRET_SET_ORDER in that case ) ... 您知道了,但是如果您需要帮助,请询问(在这种情况下,最好删除PRET_SET_ORDER)...

this would be my suggested end structure for a css file.. 这将是我建议的CSS文件的最终结构。

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

I think preg_match_all doesn't get the attention it needs, it can be really useful for this kind of thing, if your not familiar with it check it out at the php doc http://www.php.net/manual/en/function.preg-match-all.php 我认为preg_match_all并没有得到需要的关注,如果您不熟悉它,可以在php doc http://www.php.net/manual/zh/上查看,它对于此类事情可能确实有用。 function.preg匹配,all.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个正则表达式,在Apache上运行正常,但在Nginx上失败 - A regex expression, that works fine on Apache, but fails on nginx PHP页面无法在Bluemix上运行,但是在我的本地Web服务器上可以正常工作 - PHP page not working on Bluemix, but works fine on my local webserver 正则表达式在测试时效果很好,但不在我的页面中 - Regex expression works well when testing but not in my page php登录无法在线工作,但可以在离线状态下正常运行 - php log in not working online but works perfectly fine offline 正则表达式测试人员返回结果,但不返回我的网站 - Regex testers return results but not my site PHP regex无法在我的网站上运行,但可以在正则表达式编辑器上运行 - PHP regex doesn't working in my website but works on regular expression editor 为什么我的htaccess无法在我的Web服务器上运行(但是在本地运行良好)? - Why is my htaccess not working on my web server (but it works fine locally)? Captcha 在 localhost 上工作正常,但不能在线 - Captcha is working fine on localhost , but not online 自定义页面模板在本地主机上工作正常,但无法在线显示 - Custom page template works fine on local host but doesn't show up online WordPress Contact Form 7正则表达式验证的处理方式不同于在线正则表达式测试器? - WordPress Contact Form 7 Regex validation handles differently than online regex testers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM