【问题标题】:Search mysql with wildcard and put results into php array使用通配符搜索 mysql 并将结果放入 php 数组
【发布时间】:2017-02-11 23:26:51
【问题描述】:

假设我有this is the RED colorthis is the BLUE colorthis is WHATEVER color 的出现,作为它们所属的不同句子的一部分,在表 table 的列 column 的不同单元格中 我的 mySQL 数据库,或者如果这个表在同一行。

----------------------------------------------------------------------------------------
| 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. 这是水色

跳过相应句子的所有其他部分?我的意思是我只需要将 这是 CURRENT_WILDCARD_VALUE 颜色 的每一个外观都放入数组中,仅此而已。

到目前为止,我有以下 PHP 代码

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

它返回大量额外数据,而我需要它只返回我的搜索词,记住它使用的通配符。

【问题讨论】:

  • 您可以使用likeregexp。是否只允许thecolor 之间的一系列字母字符?如果是这样,我认为regexp 'this is the [a-zA-Z]+ color 会这样做。如果颜色总是大写取出a-z(mysql 正则表达式也可能不区分大小写,所以也许没关系)
  • 带有标点符号和括号(方和圆)的扩展拉丁字母就足够了,或者您的意思是什么?结果可能很容易成为this it the LIGHT BLUE color。问题是我也不知道如何创建查询并将结果放入 php 数组中。
  • 你目前有什么?解释如何查询和获取有点宽泛..
  • 你能用 db 有什么和你想让 PHP 得到什么来更新这个问题吗?
  • 这就是查询,你能显示一个实际列的值以及你想从中得到什么吗?

标签: php mysql


【解决方案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);

$matches 的值应该是:

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

如果您想保留一组唯一值(例如红色),最简单的方法是将每个 $matches[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',
)

【讨论】:

  • 我如何填写$myValue
  • 我不确定 wpdb->get_results 返回什么,但我认为它是一个结果列表。所以你可以这样做: foreach ($wpdb->get_results() as $myValue) { put stuff in here> }
【解决方案2】:

最后我得到了以下似乎符合我期望的代码:

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

在此示例中,我已将 green 严格设置为 $srchtrm 值,但 $srchtrm 实际上是一个动态变量,所以一切正常,我不想创建一个只有一个键的数组。

代码一切正常还是可能存在一些隐藏的(对我而言)错误?

【讨论】:

    猜你喜欢
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    相关资源
    最近更新 更多