2017-02-11 28 views
3

說我有this is the RED color出場,this is the BLUE colorthis is WHATEVER color因爲它們屬於,在表中的列的不同細胞的不同句子的一部分我mySQL數據庫,或者如果這個表在同一行。搜索通配符MySQL和把成果轉化爲PHP數組

---------------------------------------------------------------------------------------- 
| column                    | 
---------------------------------------------------------------------------------------- 
| Look, this is the red color which is is not a nice one.        | 
---------------------------------------------------------------------------------------- 
| And this is the blue color, I like it more.           | 
---------------------------------------------------------------------------------------- 

我怎麼能執行在數據庫中搜索this is the % color的精確匹配,並把結果放到PHP數組,這樣我就可以輸出數組正是

  1. 這是紅色的

  2. 這是藍色

  3. 這是WATEVER顏色

跳過各個句子的其他部分?我的意思是我需要將陣列只放入的每一個外觀,這是CURRENT_WILDCARD_VALUE顏色,僅此而已。

到目前爲止,我有以下的PHP代碼

global $wpdb; 
$query = $wpdb->get_results("SELECT * FROM table WHERE row LIKE '% this is the % color %'"); 
print_r($query); 

,而我需要它返回的腦子裏只有我的搜索詞保持它使用通配符返回萬噸的額外數據。

+1

你可以使用'like'或'regexp'。你想只允許''''和'顏色'之間的一系列字母字符?如果是這樣,我認爲'regexp'這是[a-zA-Z] +顏色'會做到這一點。如果顏色始終是帽子,請取出'a-z'(mysql正則表達式可能不區分大小寫,所以也許它沒有關係) – chris85

+0

帶有標點符號和方括號(正方形和圓形)的擴展拉丁字母將足夠了,或者您是什麼意思?結果可能很容易成爲「這是淺藍色」。問題是我也不知道如何創建查詢並將結果放入php數組中。 – Pimpfru

+1

你目前有什麼?解釋如何查詢和獲取有點寬.. – chris85

回答

1

最後我已經結束了與下面的代碼,這似乎符合我的期望:

global $wpdb; 

// setting up the database search term 
$srchtrm = "green"; 

// querying WP database for matches and putting their parent cells into array 
$query = $wpdb->get_results(" 
    SELECT post_content 
    FROM wp_posts 
    WHERE post_content 
    REGEXP 'This is the $srchtrm color' 
    "); 

// simplifying the query resulting array 
foreach ($query as $value) {$simplified_array[] = $value->post_content;} 

// avoiding php's Warning: implode(): Invalid arguments passed in 
if (empty($simplified_array)) {} 
    // converting the simplified array into a single text string 
    else {$textstring = implode(" ", $simplified_array);} 

// searching the above text string for matches and putting them into array 
preg_match_all('/This is the '.$srchtrm.' color/', $textstring, $matches); 

// removing repeating items in simplified array 
$simplifiedmatches = array_unique($matches[0]); 

// sorting the simplified matches array asc 
sort($simplifiedmatches); 

// outputting values 
foreach($simplifiedmatches as $match) {echo $match.'<br />';} 

我嚴格設置​​作爲$srchtrm在這個例子中的值,但$srchtrm實際上是一個動態變量,所以一切都很好,我不想創建一個只有一個鍵的數組。

一切正常的代碼或可能是一些隱藏的(對我來說)錯誤存在?

1

一旦你從MySQL中選擇了你的值。通過preg_match()傳遞每個記錄值。事情大致如下所示:

$myValue = 'Look, this is the red color which is is not a nice one.'; // Value from MySQL 
$matches = array(); 
preg_match ('/this is the (.+) color/', $myValue, $matches); 

的$值相匹配應該是:

array (
    0 => 'this is the red color', 
    1 => 'red', 
) 

然後,如果您想保留一組唯一值的(如紅色)的最簡單的是存儲每個$匹配[1]作爲數組鍵。例如。

$myColors = array(); // Put this before record iterator 
$myColors[$matches[1]] = $matches[0]; 

將產生的數組:

array (
    'red' => 'this is the red color', 
    'blue' => 'this is the blue color', 
) 
+0

如何填寫'$ myValue'? – Pimpfru

+1

我不確定wpdb-> get_results會返回什麼,但我認爲它是結果列表。所以你可以這樣做:foreach($ wpdb-> get_results()as $ myValue){把東西放在這裏>} – Detritus