我需要實現以下功能:交換空間中找到的字符串php
我有一個名稱字段,其中包含名稱和姓氏。這存儲在數據庫中,並按照'姓氏名'的順序。
我正在執行一個腳本來搜索這些記錄。目前,我設法檢查一個字符串是否包含一個空格,如果它包含一個空格,則表示它是一個名字,而不是一個ID卡號。下面是代碼:
$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
}
然後我將插入SQL語句中的這些字符串與LIKE屬性會有一個像李四和DOE JOHN兩者。因此,如果用戶可以輸入John Doe或Doe John來查找結果。
有關如何做到這一點的任何建議?
非常感謝
克里斯
,如果你想找到「喬納森·多伊」和「約翰·亨利·多伊」而定。它也不會在字符串的開頭使用帶有通配符的索引。 – nickf 2009-12-03 10:46:18