在php中找到空格的位置交换字符串

在php中找到空格的位置交换字符串,php,string,Php,String,我需要实现以下功能: 我有一个名称字段,它包含名称和姓氏。这存储在数据库中,顺序为“姓氏名称” 我正在实现一个脚本来搜索这些记录。目前,我设法检查字符串是否包含空格,如果它包含空格,则意味着它是一个名称,而不是身份证号码(例如)。代码如下: $query = "John Doe"; $checkIfSpaceExists = strpos($query, " "); if ($checkIfSpaceExists == "") { //No Space therefore it is

我需要实现以下功能:

我有一个名称字段,它包含名称和姓氏。这存储在数据库中,顺序为“姓氏名称”

我正在实现一个脚本来搜索这些记录。目前,我设法检查字符串是否包含空格,如果它包含空格,则意味着它是一个名称,而不是身份证号码(例如)。代码如下:

$query = "John Doe";

$checkIfSpaceExists = strpos($query, " ");

if ($checkIfSpaceExists == "")
{
    //No Space therefore it is not a name
}
else
{
    //Contains space
   $queryExploded = explode(" ", $query);
   foreach ($queryExploded as $q)
   {
      //Here I need the functionality so that if someone entered John Doe
      //2 different strings are saved, which are
      //$string1 = John Doe
      //$string2 = Doe Johns

      //If the name consists of 3 parts, strings for every combination is saved
}
   $like = ""; //This is the LIKE sql command.
   foreach ($selectedFields as $field)
   {
            $checkIfSpaceExists = strpos($query," ");

    if ($checkIfSpaceExists != "")
    {
        $query1 = $query; //No space, so query1 is equal ta original query

        $queryExploded = explode(" ", $query);

        for ($i=0; $i<count($queryExploded); $i++) //First loop (name surname)
        {
            $tmp1 = $tmp1 . " " . $queryExploded[$i];
        }

        for ($i=count($queryExploded); $i>=0; $i--) //Second loop (surname name)
        {
            $tmp2 = $tmp2 . " " . $queryExploded[$i];

        }
        $query2 = $tmp2;
        $query2 = trim($query2);

        $like = $like . $field . " LIKE '%" . $query1 . "%' or " . $field . " LIKE '%" . $query2 . "%' or ";
然后我将这些字符串插入到一个具有LIKE属性的SQL语句中,johndoe和doejohn都会有LIKE。因此,如果用户可以输入John Doe或Doe John来查找结果

有什么建议吗

非常感谢


chris

在单独和组合的LIKE约束中使用这些分解的3个字符串怎么样

差不多

"... WHERE name LIKE '%$name[0]%' AND name LIKE '%$name[1]%' AND name LIKE '%$name[2]%'"

您可以在foreach循环中构建此字符串。

在单独和组合的LIKE约束中使用这些分解的3个字符串怎么样

差不多

"... WHERE name LIKE '%$name[0]%' AND name LIKE '%$name[1]%' AND name LIKE '%$name[2]%'"

您可以在foreach循环中构建此字符串。

好的,从头开始-请务必仔细阅读<代码>strpos并没有完全按照您认为的那样执行。以下是检查空间的方法:

if (strpos($query, ' ') === false)  // the triple-equals is important!

在那之后,这只是一个简单的问题。下面是关于堆栈溢出的另一个答案,它向您展示了如何做到这一点:

好的,从一开始-请务必仔细阅读<代码>strpos并没有完全按照您认为的那样执行。以下是检查空间的方法:

if (strpos($query, ' ') === false)  // the triple-equals is important!
在那之后,这只是一个简单的问题。下面是关于堆栈溢出的另一个答案,它向您展示了如何执行此操作:

多伊·约翰


Doe John

我想你不能把这个字段分为姓名和姓氏?我建议在数据库中创建新的表和标记。标签将是任何单词-可能是姓氏,可能是姓名,可能是身份证号码。。。然后用record\u id和tag\u id创建一个连接record\u tags

SELECT record.* FROM record
INNER JOIN record_tags rt1 ON rt1.record_id = record.id
INNER JOIN tag t1 ON t1.id = rt1.tag_id
INNER JOIN record_tags rt2 ON rt2.record_id = record.id
INNER JOIN tag t2 ON t2.id = rt2.tag_id
WHERE
t1.name = "John"
AND t2.name = "Doe"

这将是更好的搜索方法,因为您可以使用任意数量的单词/标记。我认为SQL可以更简单。我认为多重类方法的速度要慢得多,尤其是随着数据库的增长。

我想您不能将此字段拆分为名称和姓氏?我建议在数据库中创建新的表和标记。标签将是任何单词-可能是姓氏,可能是姓名,可能是身份证号码。。。然后用record\u id和tag\u id创建一个连接record\u tags

SELECT record.* FROM record
INNER JOIN record_tags rt1 ON rt1.record_id = record.id
INNER JOIN tag t1 ON t1.id = rt1.tag_id
INNER JOIN record_tags rt2 ON rt2.record_id = record.id
INNER JOIN tag t2 ON t2.id = rt2.tag_id
WHERE
t1.name = "John"
AND t2.name = "Doe"

这将是更好的搜索方法,因为您可以使用任意数量的单词/标记。我认为SQL可以更简单。我认为multiple-like方法的速度要慢得多,尤其是随着数据库的增长。

通过一些循环和字符串操作,我成功地实现了这个脚本

以下是我所做的:

我使用strpos检查了查询是否包含空间。如果它包含一个空格,则意味着它是一个名称,同时包含名称和姓氏,因此我输入了一个循环,以便输出一个带“name-names”的字符串和另一个带“names-name”的字符串

代码如下:

$query = "John Doe";

$checkIfSpaceExists = strpos($query, " ");

if ($checkIfSpaceExists == "")
{
    //No Space therefore it is not a name
}
else
{
    //Contains space
   $queryExploded = explode(" ", $query);
   foreach ($queryExploded as $q)
   {
      //Here I need the functionality so that if someone entered John Doe
      //2 different strings are saved, which are
      //$string1 = John Doe
      //$string2 = Doe Johns

      //If the name consists of 3 parts, strings for every combination is saved
}
   $like = ""; //This is the LIKE sql command.
   foreach ($selectedFields as $field)
   {
            $checkIfSpaceExists = strpos($query," ");

    if ($checkIfSpaceExists != "")
    {
        $query1 = $query; //No space, so query1 is equal ta original query

        $queryExploded = explode(" ", $query);

        for ($i=0; $i<count($queryExploded); $i++) //First loop (name surname)
        {
            $tmp1 = $tmp1 . " " . $queryExploded[$i];
        }

        for ($i=count($queryExploded); $i>=0; $i--) //Second loop (surname name)
        {
            $tmp2 = $tmp2 . " " . $queryExploded[$i];

        }
        $query2 = $tmp2;
        $query2 = trim($query2);

        $like = $like . $field . " LIKE '%" . $query1 . "%' or " . $field . " LIKE '%" . $query2 . "%' or ";
$like=”“//这是LIKE-sql命令。
foreach($selectedFields作为$field)
{
$checkIfSpaceExists=strpos($query,“”);
如果($checkIfSpaceExists!=“”)
{
$query1=$query;//没有空格,所以query1等于原始查询
$queryExploded=分解(“,$query”);
for($i=0;$i=0;$i--)//第二个循环(姓氏名称)
{
$tmp2=$tmp2.“$queryexploed[$i];
}
$query2=$tmp2;
$query2=修剪($query2);
$like=$like.$field.“像“%”“%$query1.”或“%$field.”像“%”“%$query2.”或“%$field.”;

我希望这能帮助其他需要帮助的人:)

通过一些循环和字符串操作,我成功地实现了这个脚本

以下是我所做的:

我使用strpos检查了查询是否包含空格。如果它包含空格,则意味着它是一个名称,同时包含名称和姓氏,因此我输入了一个循环,以便输出一个带“name-姓氏”的字符串和另一个带“姓氏”的字符串

代码如下:

$query = "John Doe";

$checkIfSpaceExists = strpos($query, " ");

if ($checkIfSpaceExists == "")
{
    //No Space therefore it is not a name
}
else
{
    //Contains space
   $queryExploded = explode(" ", $query);
   foreach ($queryExploded as $q)
   {
      //Here I need the functionality so that if someone entered John Doe
      //2 different strings are saved, which are
      //$string1 = John Doe
      //$string2 = Doe Johns

      //If the name consists of 3 parts, strings for every combination is saved
}
   $like = ""; //This is the LIKE sql command.
   foreach ($selectedFields as $field)
   {
            $checkIfSpaceExists = strpos($query," ");

    if ($checkIfSpaceExists != "")
    {
        $query1 = $query; //No space, so query1 is equal ta original query

        $queryExploded = explode(" ", $query);

        for ($i=0; $i<count($queryExploded); $i++) //First loop (name surname)
        {
            $tmp1 = $tmp1 . " " . $queryExploded[$i];
        }

        for ($i=count($queryExploded); $i>=0; $i--) //Second loop (surname name)
        {
            $tmp2 = $tmp2 . " " . $queryExploded[$i];

        }
        $query2 = $tmp2;
        $query2 = trim($query2);

        $like = $like . $field . " LIKE '%" . $query1 . "%' or " . $field . " LIKE '%" . $query2 . "%' or ";
$like=”“;//这是like-sql命令。
foreach($selectedFields作为$field)
{
$checkIfSpaceExists=strpos($query,“”);
如果($checkIfSpaceExists!=“”)
{
$query1=$query;//没有空格,所以query1等于原始查询
$queryExploded=分解(“,$query”);
for($i=0;$i=0;$i--)//第二个循环(姓氏名称)
{
$tmp2=$tmp2.“$queryexploed[$i];
}
$query2=$tmp2;
$query2=修剪($query2);
$like=$like.$field.“像“%”“%$query1.”或“%$field.”像“%”“%$query2.”或“%$field.”;

我希望这能帮助其他需要帮助的人:)

取决于您是否要查找“Johnathon Doe”和“John Henry Doe”。它也不会使用字符串开头带有通配符的索引。取决于您是否要查找“Johnathon Doe”和“John Henry Doe”。它也不会使用字符串开头带有通配符的索引。这不会处理多个空格(3个或更多单词)preg_replace('/^(\w+)(\w+)(\w+)(\w+)$/','$3$2$1',“John Jay Doe”);preg_replace('/^(\w+)(\w+)(\w+),'$3$1$2',“Jay John Doe”);Happy?3个部分,这就是他要求的全部内容……这不会处理多个空格(3个或3个以上单词)preg_replace(“/^(\w+)(\w+)(\w+)/”、“$3$2$1”、“John Jay Doe”);preg_replace(“/^(\w+)(\w+)/”、“$3$1$2”、“Jay John Doe”);Happy?3个部分,这是他要求的全部…如果我像你所说的那样使用它,脚本就不好用。如果我使用原来的脚本,它就很好用。我会更新我的帖子,因为我成功地实现了它。你能告诉我使用它的方式有什么问题吗?谢谢手册中有一个红色突出显示的部分,上面有一个非常大的标记警告标志,内容为:
此函数可能返回布尔值FALSE,但也可能返回非布尔值ev