Tag Archive for search

Search field content control

//
// Search form control function
//

$(document).ready(function(){

// Get default search box value from hidden label
$("#keywords").attr("value", $("#search label").text());

// Clear search box only if default value present
$("#keywords").focus(function(){
if ($(this).attr("value") == $("#search label").text()) {
$(this).attr("value", "");
}
});

});

source

Extraction Search Pattern

{*}<h2>{%}</h2>{%}</div><ul class="{*}">{%}</ul>{*}<a class="commentslink" href="{%}">{*}</a>

source

Php string in string function

// php has no "in_string" function, so here's one...

function in_string($needle, $haystack, $insensitive = 0) {
if ($insensitive) {
return (false !== stristr($haystack, $needle)) ? true : false;
} else {
return (false !== strpos($haystack, $needle))  ? true : false;
}
}

source

PHP 正規表現を使用して文字列を検索する

$str = '<a href="/index.html" target="_blank">PHPリファンレンス</a>';

//大文字・小文字を区別してマッチングを行う場合
if( ereg( "href", $str ) ){
print "href にマッチしましたn";
}

//大文字・小文字を区別しないでッチングを行う場合
if( eregi( "HREF", $str ) ){
print "HREF にマッチしました(大文字・小文字は無視)n";
}

//後方参照を行うには、第三引数に配列変数を指定する
if( ereg( "href="([^"]*)" target="([^"]*)"", $str, $matches) ){
for( $i = 0; $i < count($matches); $i++ ){
print $matches[$i] . "n";
//結果は以下の順に出力される
//href="/index.html" target="_blank"
///index.html
//_blank
}
}

source

Google Custom Search Engine from a List of Links

<!-- Use of this code assumes agreement with the Google Custom Search Terms of Service. -->
<!-- The terms of service are available at /coop/docs/cse/tos.html -->
<form name="cse" id="searchbox_demo" action="http://www.google.com/cse">
<input type="hidden" name="cref" value="" />
<input name="q" type="text" size="40" />
<input type="submit" name="sa" value="Search" />
</form>
<script type="text/javascript" src="http://www.google.com/cse/tools/onthefly?form=searchbox_demo"></script>

source

Recursive grep

grep pattern -r directory *.txt

source

Basic Uncommented Crappy Binary Radix Trie

class Fixnum
def to_b(l = <img src='http://www.snippetsmania.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> "0′" + self.to_s(2).rjust(l, "0")
end
def set?(i)
if((self & (1 << i)) != 0)
return true
else
return false
end
end

def bdiff(x)
ret = -1
32.downto(0) do |i|
if(self.set?(i) != x.set?(i))
ret = i
break
end
end
ret
end
end

class String
def to_ip
ip = 0
split(".").each_with_index do |octet, index|
ip |= (octet.to_i << ((3 - index)*8))
end
ip
end
end

class SimpleTrie
def initialize(width=32)
@@node ||= Struct.new(:right, :left, :pos, :key, :value, :color)
@root = @@node.new(nil, nil, width, 0, nil)
@root.right = @root
@root.left = @root
@width = width
@sz = 0
end

private

def srch(key, limit=nil)
cur = @root
prev = nil

while(((not prev) or (cur.pos < prev.pos)) and ((not limit) or cur.pos > limit))
prev = cur
if(key.set? cur.pos)
cur = cur.right
else
cur = cur.left
end
end

return cur, prev
end

public

def []=(key, val)
x, prev = srch(key)
bit = key.bdiff(x.key)
cur, prev = srch(key, bit)

node = @@node.new(nil, nil, bit, key, val)

if(key.set? bit)
node.right = node
node.left = cur
else
node.right = cur
node.left = node
end

if(key.set? prev.pos)
prev.right = node
else
prev.left = node
end

@sz += 1

return val
end

def walk
color = rand
walker = lambda do |x, dir, tab|
if(x.color != color)
tab.times { print "  " }
puts "#{ dir } #{ x.key } ( #{ x.key.to_b } ) @ #{ x.pos }"

x.color = color

walker.call(x.right, "-> ", tab+1)
walker.call(x.left, "<- ", tab+1)
end
end

walker.call(@root, ".   ", 0)
end

def [](key)
res, p = srch(key)
return res.value
end
end

source

Exclude some directories from grep

find . -name "*.html" ( -name 'dirFoo' -prune -o  -name 'dirBar' -prune ) | xargs grep -n "THE_PATH_YOU_ARE_LOOKING_FOR"

source

MySQL search

Word searching

1.

SELECT * FROM TABLE WHERE MATCH (`field`) AGAINST ('Keyword')

(Fastest)

2.

SELECT * FROM TABLE WHERE MATCH (`field`) AGAINST ('+Keyword' IN BOOLEAN MODE)

(Fast)

3.

SELECT * FROM TABLE WHERE RLIKE '(^| +)Keyword($| +)'

OR

SELECT * FROM TABLE WHERE
RLIKE '([[:space:]]|[[:<:]])Keyword([[:space:]]|[[:>:]])'

(Slow)
Contains searching

1.

SELECT * FROM TABLE WHERE MATCH (`field`) AGAINST ('Keyword*' IN BOOLEAN MODE)

(Fastest)

2.

SELECT * FROM TABLE WHERE FIELD LIKE 'Keyword%'

(Fast)

3.

SELECT * FROM TABLE WHERE MATCH (`field`) AGAINST ('*Keyword*' IN BOOLEAN MODE)

(Slow)

4.

SELECT * FROM TABLE WHERE FIELD LIKE '%Keyword%'

(Slow)
Recordsets

1.

SELECT SQL_CALC_FOUND_ROWS * FROM TABLE WHERE Condition LIMIT 0, 10
SELECT FOUND_ROWS()

(Fastest)

2.

SELECT * FROM TABLE WHERE Condition LIMIT 0, 10
SELECT COUNT(PrimaryKey) FROM TABLE WHERE Condition

(Fast)

3.

$result = mysql_query("SELECT * FROM table", $link);
$num_rows = mysql_num_rows($result);

(Very slow)
Joins

Use an INNER JOIN when you want the joining table to only have matching records that you specify in the join. Use LEFT JOIN when it doesn’t matter if the records contain matching records or not.

SELECT * FROM products
INNER JOIN suppliers ON suppliers.SupplierID = products.SupplierID

Returns all products with a matching supplier.

SELECT * FROM products
LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID
WHERE suppliers.SupplierID IS NULL

Returns all products without a matching supplier.

source

Scrape Google from the command line

perl -e "$i=0;while($i<1000){sleep 1; open(WGET,qq/|xargs lynx -dump/);printf WGET qq{http://www.google.com/search?q=site:onemorebug.com&hl=en&start=$i&sa=N},$i+=10}" | grep "//[^/]*onemorebug.com/"

source