preg_match
成都創(chuàng)新互聯(lián)公司長(zhǎng)期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為永定企業(yè)提供專業(yè)的網(wǎng)站制作、成都網(wǎng)站制作,永定網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
(PHP 4, PHP 5)
preg_match — Perform a regular expression match
Report a bug
Description
int preg_match ( string $pattern , string $subject [, array $matches [, int $flags = 0 [, int $offset = 0 ]]] )
Searches subject for a match to the regular expression given in pattern.
Report a bug
Parameters
pattern
The pattern to search for, as a string.
subject
The input string.
matches
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
flags
flags can be the following flag:
PREG_OFFSET_CAPTURE
If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
/*** $array---需要被的數(shù)組* $str --- 要查詢的字符串* $return -- 返回匹配該字符串的數(shù)組的下標(biāo)集合。*/public function search($array,$str){ foreach($all as $key=$val){ $retrun = array(); // 如果$key中含有$str字符串,那么添加到數(shù)組$return中 if(stripos($key,$str) != false || stripos($key,$str) == '0') { array_push($retrun ,$key); }}return $return;}
mysql支持自然語言的全文搜索
對(duì)于字段的要求:
只能是CHAR, VARCHAR, 或 TEXT 類型的字段
表類型是MyISAM
在表建好,并導(dǎo)入數(shù)據(jù)后,建立一個(gè)fulltext index(索引)
用法:
select 字段1,字段2,字段3, MATCH(要匹配的字段名) AGAINST('keyword') as score from table having score0
命中的每一行都會(huì)有個(gè)分?jǐn)?shù),分?jǐn)?shù)越大表示結(jié)果越接近keyword,分?jǐn)?shù)越低的就是越模糊的結(jié)果
模糊匹配:like %關(guān)鍵詞%
完全匹配:select 語句 直接等于關(guān)鍵詞