在PHP字符串中正确形式的不定冠词(a,an) [英] Correct form of indefinite article (a, an) in PHP strings

查看:93
本文介绍了在PHP字符串中正确形式的不定冠词(a,an)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来替换字符串中的/ an来同意下面的单词 - 与S在日期格式中的工作方式一样?

Is there an easy way to substitute a/an in a string to agree with the following word - much the same as the way 'S' works in Date format?

$apple = 'apple';
$pear = 'pear';
echo "This is a $apple, this is a $pear."

--> This is an apple, this is a pear


推荐答案

$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";
}

输出:

an apple is a fruit
a banana is also a fruit

这篇关于在PHP字符串中正确形式的不定冠词(a,an)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆