2017-02-11 11 views
3

に結果を入れて、私のテーブルテーブルの列の異なるセルに、彼らが属する異なる文章の一部としてthis is WHATEVER color、私はthis is the RED colorの外見、this is the BLUE colorを持っていると言いますmySQLデータベース、またはこのテーブルの場合は同じローです。そのように、私は可能性があり、出力配列正確 検索MySQLとPHPの配列

よう
  1. 私はこれ、完全に一致するthis is the % colorのためにデータベースに検索を実行し、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.           | 
    ---------------------------------------------------------------------------------------- 
    

    赤色です

  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'を使うことができます。 'the'と' color'の間にアルファベットの一連の文字を許可したいですか?もしそうなら、私は 'regexp 'これは[a-zA-Z] + color'であると思います。もし色が常に 'a-z'を取り出したら(mysql正規表現は大文字と小文字を区別しないかもしれません) – chris85

+0

句読点と角括弧(角と丸)を入れた拡張ラテンアルファベットでは十分でしょうか?結果は簡単に「これはライトブルーの色です。問題は、私もどのようにクエリを作成し、PHPの配列に結果を置くか分からないです。 – Pimpfru

+1

あなたは現在何を持っていますか?クエリとフェッチの仕方を少し説明しています。 – chris85

答えて

1

:の配列を生成します

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

この例では$srchtrmの値として​​と表示されていますが、$srchtrmは実際には動的変数なので、すべてが問題なく、1つのキーだけの配列を作成しようとはしていません。

コードに問題はありませんか、いくつかのエラーが存在する可能性がありますか?

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]にマッチします。例えば。

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 />';} 

私は厳密に設定しました:最後に、私は私の期待に応えるように見える次のコードになってしまってきた

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

'$ myValue'はどうすればいいですか? – Pimpfru

+1

wpdb-> get_resultsが返すものはわかりませんが、結果のリストだと思います。つまり、foreach($ wpdb-> get_results()を$ myValue){ここに入れる>} – Detritus