我想在PHP函數中格式化名稱。名稱格式爲Forname SURNAME
,我需要格式Forename Surname
。PHP strpos()返回錯誤的結果
我寫的函數很好地完成了這項工作,但遇到如O'HARE
這樣的姓時,當所需的格式爲O'Hare
時,返回的名稱是O'hare
。
爲了實現這個條件,我用下面的代碼:
...
if(stripos($name, "O'") != false)
{
$nameArr = array();
$expl3 = explode(' ', trim($name));
print_r($expl3); //this prints [0] => Forename [1] => O'SURNAME as expected
foreach($expl3 as $ex)
{
if(stripos($ex, "O'") != false)
{
echo '<h1>string position found</h1>'; //this condition is not being met
$newName = str_replace("O'", '', ($ex));
$nameArr[] = "O'" . ucfirst(strtolower($newName));
}
else
{
$nameArr[] = ucfirst(strtolower($ex));
}
}
$formattedName = implode(' ', $nameArr);
}
else
{
$formattedName = trim(ucwords(strtolower($name)));
}
return $formattedName;
功能尚在格式Forename O'SURNAME
返回名稱。
我的問題是,爲什麼是第二stripos()
沒有找到O'
它事實上是存在的,當我print_r()
當很明顯?