Leo*_*Leo 6 php string cakephp
是否有一种简单的方法来替换字符串中的/ a以符合以下单词 - 与'S'在日期格式中的工作方式非常相似?
例如
$apple = 'apple';
$pear = 'pear';
echo "This is a $apple, this is a $pear."
--> This is an apple, this is a pear
Run Code Online (Sandbox Code Playgroud)
试试这个 :
$l = array('a apple is a fruit', 'a banana is also a fruit');
foreach($l as $s) {
$s = preg_replace('/(^| )a ([aeiouAEIOU])/', '$1an $2', $s);
echo $s,"\n";
}
Run Code Online (Sandbox Code Playgroud)
输出:
an apple is a fruit
a banana is also a fruit
Run Code Online (Sandbox Code Playgroud)
您可以使用正则表达式来交换a /,具体取决于它后面的内容.更棘手的部分实际上将定义所有要交换的案例 - 它比'如果它后跟元音'更复杂.
何时使用/ an:
使用以辅音声音开头的之前的单词/缩写; 使用以元音开头的单词/缩写词.这是基于发音而不是拼写.
因此:
开始使用正则表达式来解决它
$text = preg_replace("/(?=a|e|i|o|u|yt)a/", "an", $text);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1927 次 |
最近记录: |