php 用explode和foreach分解字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1459428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
breaking up strings with explode and foreach
提问by Neil Hickman
I'm Trying to get a php string to break down into individual words, search for them in a database and then add the results into another database..
我正在尝试将 php 字符串分解为单个单词,在数据库中搜索它们,然后将结果添加到另一个数据库中..
I currently have the string like this
我目前有这样的字符串
"SDGCC, ARISE, SDGFS"
“SDGCC,崛起,SDGFS”
I want to search in the login tags table for tags LIKE these words i'm using this
我想在登录标签表中搜索标签,比如我正在使用的这些词
SELECT * FROM
logintagsWHEREtagLIKE '%string%'
SELECT * FROM
logintagsWHEREtagLIKE '%string%'
so that each tag is taken from the string i've tried using explode and foreach but it doesn't seem to be getting the desired effect.
这样每个标签都是从我尝试使用爆炸和 foreach 的字符串中获取的,但它似乎没有得到预期的效果。
$string = $_POST['tags'];
$tags = explode(',' $string);
foreach($tags as $key) {
$query = mysql_query("SELECT * FROM
dept_logintagsWHEREtagLIKE '%".$key."%'"); $r = mysql _fetch _object($query);$insert = mysql_query("INSERT INTO
briefings_logintags(id,briefing_id,logintag_id) VALUES (NULL, '$id', '".$r->id."')");}
$string = $_POST['tags'];
$tags = expand(',' $string);
foreach($tags as $key) {
$query = mysql_query("SELECT * FROM
dept_logintagsWHEREtagLIKE '%".$key."%'"); $r = mysql _fetch _object($query);$insert = mysql_query("INSERT INTO
briefings_logintags(id,briefing_id,logintag_id) VALUES (NULL, '$id', '".$r->id."')");}
the effect i'm trying to create is for each tag create a reference between the tag id and the briefing id...
我试图创建的效果是为每个标签在标签 ID 和简报 ID 之间创建一个引用...
However it seems only to be putting one correct entry in the database.
然而,它似乎只是在数据库中放入一个正确的条目。
采纳答案by ctford
If there is a space after the comma in your original string, then explode() will return something like ["SDGCC", " ARISE", " SDGFS"].
如果原始字符串中的逗号后面有一个空格,则explode() 将返回类似["SDGCC", " ARISE", " SDGFS"] 的内容。
This will cause your LIKE clause to only match the first word. Try using trim():
这将导致您的 LIKE 子句只匹配第一个单词。尝试使用修剪():
$word=trim($key);
$safe=mysql_real_escape_string( $word );
$query=mysql_query("SELECT * FROM dept_logintags WHERE tag LIKE '%".$safe."%'");
EDIT: As Artelius mentioned, always escape user-submitted data to prevent SQL injection.
编辑:正如 Artelius 提到的,总是转义用户提交的数据以防止 SQL 注入。
回答by Artelius
Try
尝试
$tags = explode(',',$string);
foreach($tags as $key) {
echo '"'.$key.'"<br/>';
}
My guess is that you are getting a space before ARISE and SDGFS.
我的猜测是您在 ARISE 和 SDGFS 之前获得了一个空间。
Also, make sure you always escape stringsproperly before putting them into MySQL queries!! Otherwise someone can send tainted input to your server and wreak havoc on your database. (This is called a SQL injection.)
另外,请确保在将字符串放入 MySQL 查询之前始终正确转义字符串!!否则,有人可以将受污染的输入发送到您的服务器并对您的数据库造成严重破坏。(这称为SQL 注入。)

