2012年12月23日 星期日

[PHP] Highlight search result


一般我們在做搜尋時,輸出的結果都會對 keyword 做 highlight 來凸顯比對結果
像字串 How to highlight search result 如果以 highlight, result 為 keyword 來搜尋
我們一般期待輸出的結果為 How to <span class=highlight>highlight</span> search <span class=highlight>result</span>
如果單純以 str_replace 用迴圈來對每個 keyword 比對時,在跑第二次也會發生問題
或是用 array 來建立 keys 和 replace 也會有點麻煩

一個簡單的寫法是使用 preg_replace 來處理

以下是範例
<style>
    .highlight {color:red}
</style>
<?php
$keys[] = 'highlight';
$keys[] = 'result';
$text = 'How to highlight search result.';

echo findKeywords_org($text, $keys) . '<br>';
echo findKeywords($text, $keys) . '<br>';

function findKeywords_org($text, $keys)
{
$replace = array();
foreach($keys as $k)
{
$replace[] = "<span class=highlight>$k</span>";
}
return str_ireplace($keys, $replace, $text);
//這是 str_replace 不分大小寫的版本,但限定 php5 以上才有,故在 php4 要不分大寫法也須要自行處理,會很麻煩
}

function findKeywords($text, $keys)
{
    $text = htmlspecialchars($text);
    $patterns = '/('. implode('|', $keys) .')/i'; //i 是不分大小寫
    $replacements = '<span class=highlight>${0}</span>'; //${0}所以比對 patterns 到的字串 value
    return preg_replace($patterns, $replacements, $text);
}



Ref: http://php.net/manual/en/function.preg-replace.php

2012年12月22日 星期六

[JavaScript] Variable dump

一個簡單的方法是
console.log(object);

另一個是寫個 dump 函數
function dump(obj) {
         var out = '';
         for (var i in obj) {
                 if (typeof obj[i] === "object")
                         out += i + ": {\n" + dump(obj[i]) + "}\n";
                 else
                         out += i + ": " + obj[i] + "\n";
         }
         return out;
}


Example:
<script>
        var book = {
                name: 'Javascript tutorial',
                price: 50,
                index: {
                        1: 'introduction',
                        2: 'hello world'
                },
                toString: function () { return "name: " + this.name + ",price: " + this.price; }
        }
        function dump(obj) {
                var out = '';
                for (var i in obj) {
                        if (typeof obj[i] === "object")
                                out += i + ": {\n" + dump(obj[i]) + "}\n";
                        else
                                out += i + ": " + obj[i] + "\n";
                }
                return out;
        }
        console.log(book);
        console.log(dump(book));
</script>



[PHP] PHP array to Javascript json object


<?php

        $books = array();
        $books[] = array('name'   => 'php tutorial',
                         'author' => 'php',
                         'price'  => 100
                        );
        $books[] = array('name' => 'javascript tutorial',
                         'author' => 'javascript',
                         'price'=> 50
                        );

?>
<script>
        var books = <? echo json_encode($books) ?>;
        for(var i=0; i<books.length; i++)
        {
                console.log('name: '   + books[i].name);
                console.log('author: ' + books[i].author);
                console.log('price: '  + books[i].price);
        }
</script>

2012年12月4日 星期二

[CSS] CSS 選擇器

筆記起來...
ref: http://www.w3.org/TR/CSS2/selector.html




PatternMeaningDescribed in section
*Matches any element.Universal selector
EMatches any E element (i.e., an element of type E).Type selectors
E FMatches any F element that is a descendant of an E element.Descendant selectors
E > FMatches any F element that is a child of an element E.Child selectors
E:first-childMatches element E when E is the first child of its parent. The :first-child pseudo-class
E:link
E:visited
Matches element E if E is the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited). The link pseudo-classes
E:active
E:hover
E:focus
Matches E during certain user actions. The dynamic pseudo-classes
E:lang(c) Matches element of type E if it is in (human) language c (the document language specifies how language is determined). The :lang() pseudo-class
E + FMatches any F element immediately preceded by a sibling element E.Adjacent selectors
E[foo]Matches any E element with the "foo" attribute set (whatever the value). Attribute selectors
E[foo="warning"]Matches any E element whose "foo" attribute value is exactly equal to "warning". Attribute selectors
E[foo~="warning"]Matches any E element whose "foo" attribute value is a list of space-separated values, one of which is exactly equal to "warning". Attribute selectors
E[lang|="en"]Matches any E element whose "lang" attribute has a hyphen-separated list of values beginning (from the left) with "en". Attribute selectors
DIV.warningLanguage specific. (In HTML, the same as DIV[class~="warning"].) Class selectors
E#myidMatches any E element with ID equal to "myid".ID selectors

[PHP] PHP 型態比較表

筆記起來...
ref: http://php.net/manual/en/function.empty.php


Comparisons of $x with PHP functions
Expression gettype() empty() is_null() isset() boolean : if($x)
$x = ""; string TRUE FALSE TRUE FALSE
$x = null NULL TRUE TRUE FALSE FALSE
var $x; NULL TRUE TRUE FALSE FALSE
$x is undefined NULL TRUE TRUE FALSE FALSE
$x = array(); array TRUE FALSE TRUE FALSE
$x = false; boolean TRUE FALSE TRUE FALSE
$x = true; boolean FALSE FALSE TRUE TRUE
$x = 1; integer FALSE FALSE TRUE TRUE
$x = 42; integer FALSE FALSE TRUE TRUE
$x = 0; integer TRUE FALSE TRUE FALSE
$x = -1; integer FALSE FALSE TRUE TRUE
$x = "1"; string FALSE FALSE TRUE TRUE
$x = "0"; string TRUE FALSE TRUE FALSE
$x = "-1"; string FALSE FALSE TRUE TRUE
$x = "php"; string FALSE FALSE TRUE TRUE
$x = "true"; string FALSE FALSE TRUE TRUE
$x = "false"; string FALSE FALSE TRUE TRUE


Loose comparisons with ==
TRUE FALSE 1 0 -1 "1" "0" "-1" NULL array() "php" ""
TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE
1 TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
0 FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE
-1 TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
"1" TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
"0" FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
"-1" TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
NULL FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE
array() FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
"php" TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE


Strict comparisons with ===
TRUE FALSE 1 0 -1 "1" "0" "-1" NULL array() "php" ""
TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
1 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
0 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
-1 FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
"1" FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
"0" FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
"-1" FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
NULL FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
array() FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
"php" FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE