Php:检测由特殊字符包围的字符串中的单个字母

时间:2016-02-22 19:21:10

标签: php regex

说我有这些字符串

tavern-o-fallon-new-york
cut-n-shoot-texas
cut-n-shoot-texas-at-o-fellon

我想检测所有出现的-o-或任何其他模式,并转换为o'

所以我可以将字符串转换为

Tavern O'Fallon New York
Cut'N'Shoot Texas
Cut'N'Shoot Texas At O'Fellon

我相信我可以使用一些常见的模式,比如O' N' N'等。

1 个答案:

答案 0 :(得分:1)

我使用str_replace:

SELECT
referralagent.firstName
,referralagent.lastName
,customer.firstName
,customer.lastName
,customer.address
,customer.postcode
,customer.profession
,customer.phoneNumWork
,customer.phoneNumMobile
,customer.phoneNumHome
,country.name
,nationality.name

    FROM 
        customer
        INNER JOIN referralagent
            ON customer.ReferralAgent_idreferralAgent = referralagent.idReferralAgent
        INNER JOIN country
            ON customer.countryResidence = country.idCountry
        INNER JOIN nationality
            ON customer.nationality = nationality.idNationality

            WHERE customer.idUser = '7'

输出:

$phrase = 'tavern-o-fallon-new-york
cut-n-shoot-texas
cut-n-shoot-texas-at-o-fellon';
$phrase = str_replace(array('-n-','o-','-'),array('`N`','O`',' '), $phrase);
echo ucwords($phrase);

修改

修复小写字母:

Tavern O`fallon New York
Cut`N`shoot Texas
Cut`N`shoot Texas At O`fellon