PHP函数仅用于大写字典中的单词

时间:2016-02-21 01:57:06

标签: php function

我想要一个函数只大写文本文件中没有包含的单词(我自己的字典)。

function capitalize($x, $e ='utf-8') {
        if (function_exists('mb_strtoupper') && function_exists('mb_substr') && !empty($x)) {
            $x = mb_strtolower($x, $e);
            $upper = mb_strtoupper($x, $e);
            preg_match('#(.)#us', $upper, $matches);
            $x = $matches[1] . mb_substr($x, 1, mb_strlen($x, $e), $e);
        } else {
            $x = ucfirst($x);
        }
        return $x;
    } 

此代码可用于大写第一个字母。但是我希望它能够将每个单词与文本文件进行比较,并且只将该文件中存在的单词大写,并将其他单词保留为同一文本中的单词。

例如,作为字符串:

ACCESS PONIT WIRELESS-N600 DUAL BAND WITH POE SMB

文字档案

with
access
ponit

结果:

Access ponit WIRELESS-N600 DUAL BAND with POE SMB

现在我在另一个post

上看到了这段代码
<?php
$major = 'accounting technology/technician and bookkeeping';
$major = preg_replace_callback("/[a-zA-Z]+/",'ucfirst_some',$major);
echo ucfirst($major);

function ucfirst_some($match) {
   $exclude = array('and','of','the');
   if ( in_array(strtolower($match[0]),$exclude) ) return $match[0];
   return ucfirst($match[0]);
}

但我不知道如何让它一起工作

0 个答案:

没有答案