在由逗号php分隔的字符串中的值中添加引号

时间:2022-05-04 00:25:09

I have a search box that can contain multiple values using a comma, eg Pasta, tuna, eggs

我有一个搜索框,可以使用逗号包含多个值,例如意大利面,金枪鱼,鸡蛋

Im using FULLTEXT mysql search but I need to use some kind of preg_replace to turn Pasta, tuna, eggs into 'Pasta','tuna','eggs'

我使用FULLTEXT mysql搜索,但我需要使用某种preg_replace将意大利面,金枪鱼,鸡蛋变成'意大利面','金枪鱼','蛋'

If I enter this 'Pasta','tuna','eggs' into the search box the results are correct.

如果我在搜索框中输入“Pasta”,“tuna”,“eggs”,结果是正确的。

5 个解决方案

#1


Don't use regular expressions for problems that can be solved otherwise. What you want is a simple string replacement:

不要将正则表达式用于可以解决的问题。你想要的是一个简单的字符串替换:

$string = "'" . str_replace(",", "','", $string) . "'";

You should escape quotes inside the string first, though (don't know what your escape character is, assuming it's backslash):

你应该首先在字符串中转义引号(不知道你的转义字符是什么,假设它是反斜杠):

$string = "'" . str_replace(array("'", ","), array("\\'", "','"), $string) . "'";

#2


Are you building an SQL query with the list? If so, you should take some time to make sure the resulting SQL is properly escaped as well.

您是否正在使用列表构建SQL查询?如果是这样,您应该花一些时间来确保生成的SQL也被正确转义。

$myList = "pasta, tuna, eggs";

$items = preg_split("/[,\\s]+/", $myList);
$sqlItems = array();
foreach ($items as $item) {
    $sqlItems[] = "'" . mysql_real_escape_string($item) . "'";
}

// Add new list to SQL
$sql .= implode(",", $sqlItems);

#3


Do you have any comma in values?

你有值得逗号吗?

If not you could use something like:

如果没有,你可以使用类似的东西:

preg_replace('/[^,]+/g', '\'\1\'', preg_replace('/\'/g', '\\\'', $text))

#4


implode your string, then foreach resulting array and add needed symbols

破坏你的字符串,然后foreach结果数组并添加所需的符号

#5


Guys sorry for the trouble but I've solved my own question! Ive looked at this and it was all wrong to begin with.

伙计们对这个问题感到抱歉,但我已经解决了我自己的问题!我看过这个,开头就错了。

I had to replace each , with a space and a plus sign so ", tuna" = " +tuna"

我不得不用空格和加号替换每个,所以“,tuna”=“+ tuna”

Thanks anyway

#1


Don't use regular expressions for problems that can be solved otherwise. What you want is a simple string replacement:

不要将正则表达式用于可以解决的问题。你想要的是一个简单的字符串替换:

$string = "'" . str_replace(",", "','", $string) . "'";

You should escape quotes inside the string first, though (don't know what your escape character is, assuming it's backslash):

你应该首先在字符串中转义引号(不知道你的转义字符是什么,假设它是反斜杠):

$string = "'" . str_replace(array("'", ","), array("\\'", "','"), $string) . "'";

#2


Are you building an SQL query with the list? If so, you should take some time to make sure the resulting SQL is properly escaped as well.

您是否正在使用列表构建SQL查询?如果是这样,您应该花一些时间来确保生成的SQL也被正确转义。

$myList = "pasta, tuna, eggs";

$items = preg_split("/[,\\s]+/", $myList);
$sqlItems = array();
foreach ($items as $item) {
    $sqlItems[] = "'" . mysql_real_escape_string($item) . "'";
}

// Add new list to SQL
$sql .= implode(",", $sqlItems);

#3


Do you have any comma in values?

你有值得逗号吗?

If not you could use something like:

如果没有,你可以使用类似的东西:

preg_replace('/[^,]+/g', '\'\1\'', preg_replace('/\'/g', '\\\'', $text))

#4


implode your string, then foreach resulting array and add needed symbols

破坏你的字符串,然后foreach结果数组并添加所需的符号

#5


Guys sorry for the trouble but I've solved my own question! Ive looked at this and it was all wrong to begin with.

伙计们对这个问题感到抱歉,但我已经解决了我自己的问题!我看过这个,开头就错了。

I had to replace each , with a space and a plus sign so ", tuna" = " +tuna"

我不得不用空格和加号替换每个,所以“,tuna”=“+ tuna”

Thanks anyway