查找文本中的字符串 [英] Find strings in text

查看:116
本文介绍了查找文本中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 company_name列的表。现在,在一些第三方应用程序中,我收到如下短字符串:某人已将商品发送到堆栈交换

I have a table with column "company_name". Now with some third party apps I receive short strings like this: Somebody has sent item to "stack-exchange".

我想从字符串中查找具有堆栈交换company_name列的行。 (换句话说,我希望操作员可以以其他方式进行操作)。

I want to find row with stack-exchange company_name column from string. (In other words I want like operator to work the other way around). How to do this?

编辑:如何检查表中的company_name值是否与我收到的字符串的一部分匹配?

How to check if any of the values of company_name in your table match part of the string I receive?

推荐答案

编辑:由于您无法确定输入字符串的哪一部分是公司名称,因此需要检查您现有的公司名称表中针对字符串的company_name值。例如,在PHP中:

Edit: Since you can't identify what part of the input string is the company name, you need to check your existing values for company_name in your table against the string. For example, in PHP:

$input = 'Somebody has sent item to "stack-exchange"';
$result = mysql_query('SELECT company_name FROM table');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if strpos($input, $row['company_name']) === False {
        print "Company name is not in this row!" /* Substitute whatever action you want here */
    } else {
        print "Company name is in this row!" /* Substitute whatever action you want here */
    }
}

处理用户输入

请确保。否则,您会使代码容易受到黑客攻击。

Make sure that the input is sanitized before including it as part of a SQL query. Otherwise you make your code vulnerable to hacking.

不幸的是,由于您收到的某些字符串是由用户输入的,因此您无法确定它们是否匹配您的数据库中。例如,堆栈交换 可以合理地表示为 Stack Exchange 堆栈交换 StackExchange 等。因此,您需要标准化输入以匹配在数据库中存储公司名称的方式。例如,您可以将所有字符都小写,并用连字符替换所有空格或标点符号。这并不排除诸如不正确或变体拼写之类的极端情况,但这超出了此问题的范围。

Unfortunately, since some of the strings you receive are inputted by users, you can't be sure that they'll match what you have in your database. For example, "stack-exchange" could reasonably be represented as "Stack Exchange", stack exchange, StackExchange, etc. So you'll need to standardize the input to match the way you store company names in your database. For example, you could make all characters lowercase and replace all spaces or punctuation with hyphens. This doesn't rule out edge cases like incorrect or variant spellings, but that's beyond the scope of this question.


如果第三方可靠地接收到的字符串将公司名称包含在双引号中(并且双引号不用于表示这些字符串中的其他内容),您可以使用PHP检索公司名称。然后,您可以使用SQL WHERE 子句获取相关行。

$input  = 'Somebody has sent item to "stack-exchange"';
$parts = explode('"', $input);
$company_name = $parts[1];
$sql_query = 'SELECT * FROM table WHERE company_name="' . $company_name . '"'

这篇关于查找文本中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆