代码之家  ›  专栏  ›  技术社区  ›  seedg

交换在php中找到空间的字符串

  •  0
  • seedg  · 技术社区  · 15 年前

    我需要实现以下功能:

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

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

    $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属性的sql语句中,john doe和doe john都将有like。因此,如果用户可以输入john doe或doe john来查找结果。

    有什么建议吗?

    多谢

    克里斯

    5 回复  |  直到 15 年前
        1
  •  2
  •   Community CDub    8 年前

    好的,从一开始-一定要读 the manual 仔细地。 strpos 并不像你想象的那样。以下是检查空间的方法:

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

    在那之后,这只是一个问题 permutations and combinations . 下面是关于堆栈溢出的另一个答案,它向您展示了如何执行此操作: algorithm that will take number or words and find all possible combinations

        2
  •  1
  •   Zeemee    15 年前

    把这些分解后的3个字符串像约束一样单独组合在一起怎么样?

    有点像

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

    你可以在foreach循环中构建这个字符串。

        3
  •  0
  •   oliver31    15 年前
    echo preg_replace('/^(\w+) (\w+)$/', '$2 $1', "John Doe");
    

    多伊约翰

        4
  •  0
  •   Tomasz Struczyński    15 年前

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

    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可以更容易一些。我认为多重相似的方法 许多的 速度较慢,尤其是随着数据库的增长。

        5
  •  0
  •   seedg    15 年前

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

    以下是我所做的:

    我使用strpos检查了查询是否包含空格。如果它包含一个空格,则表示它的名称和姓氏都是A,因此我输入一个循环以输出一个“名称姓氏”字符串和另一个“姓氏名称”字符串

    代码如下:

       $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 ";
    

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