簡體   English   中英

正則表達式在在線測試人員中工作正常,但在我的頁面上不起作用

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

所以我正在讀取一個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\\;/''/\\:\\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 />';
}

結果:

Verdana
14
#000000

希望此解決方案有幫助!

嘗試這個:

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

更新,對不起,您將要檢查是否為空(以前我有isset),因為我看到每個變量中都設置了$ r ['selectors']。 然后,您可以通過其樣式選擇器對每個樣式塊進行子數組化,並解析為您的hart內容。 您知道了,但是如果您需要幫助,請詢問(在這種情況下,最好刪除PRET_SET_ORDER)...

這將是我建議的CSS文件的最終結構。

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

我認為preg_match_all並沒有得到需要的關注,如果您不熟悉它,可以在php doc http://www.php.net/manual/zh/上查看,它對於此類事情可能確實有用。 function.preg匹配,all.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM