Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

PHP: change "a" to "an" before vowel [was: How do I code this?]

Explorer ,
May 30, 2009 May 30, 2009

Copy link to clipboard

Copied

I have this nice little cookie: <?php echo $_COOKIE['targ_lang']; ?> . It inserts language names, like Portuguese or French into the text.

Mostly, in the text that preceeds the cookie I use the indefinite article, 'a',  'cookie', followed by something like 'newspaper'.

The code reads as:   a <?php echo $_COOKIE['targ_lang']; ?> newspaper
and it renders as: "...a Portuguese newspaper."

But sometimes the nationality starts with a vowel and that messes up the grammar of the phrase, i.e.: "...a Arabic newspaper." ick

I want a snippet that will insert an 'n' after the 'a' so it will read: "...an Arabic newspaper."

So how do I write that?

$A = A;
$E=E;
$I=I;
$O=O;
$U=U;
(I'm not worried about Yagurkistan)

<?php if $_COOKIE['targ_lang']; ?> begins with $A OR $E OR $I OR $O OR $U; echo ‘n’; ?>

I assume I assign variables to the letters A,E,I,O,U and slip this in after the 'a'. But what is the code for 'begins with'.

I'm sure glad you guys and gals are here to help and thank you!

Brian

[Subject amended by moderator to make it more meaningful]

TOPICS
Server side applications

Views

4.0K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 31, 2009 May 31, 2009

This is very simple to do. All you need to do is inspect the first letter of your variable. If it's a vowel, use "an". If not, use "a".

To get the letter at a particular position in a string, use substr(). To find if a value is in an array, use in_array().

<?php

$vowels = array('A', 'E', 'I', 'O', 'U');

if (in_array(substr($_COOKIE['targ_lang'], 0, 1), $vowels)) {

  echo 'an ';

} else {

  echo 'a ';

}

echo $_COOKIE['targ_lang']; ?>

newspaper

I have given you links to the PHP functions used in this simple s

...

Votes

Translate
LEGEND ,
May 31, 2009 May 31, 2009

Copy link to clipboard

Copied

This is very simple to do. All you need to do is inspect the first letter of your variable. If it's a vowel, use "an". If not, use "a".

To get the letter at a particular position in a string, use substr(). To find if a value is in an array, use in_array().

<?php

$vowels = array('A', 'E', 'I', 'O', 'U');

if (in_array(substr($_COOKIE['targ_lang'], 0, 1), $vowels)) {

  echo 'an ';

} else {

  echo 'a ';

}

echo $_COOKIE['targ_lang']; ?>

newspaper

I have given you links to the PHP functions used in this simple script. Please take a few moments to follow the links so that you understand how the script works. The more you learn, the easier it becomes to start writing your own scripts.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 31, 2009 May 31, 2009

Copy link to clipboard

Copied

LATEST

Thank you David! That was a great help and a big time saver. I even modified it for a second use.

Btw, I do frequently turn to the PHP manual pages but I confess that I usually come away scratching my head. I'm not a code guy. The way the manual is written, by coders for coders, it simply comes at me as info I don't need for a problem I don't recognize. Just trying to find out which area I should be looking in consumes way more time than it should, and when I find what I think I'm looking for, the problems really begin.  There are thousands of us doing websites who can only shake our heads at why the manual is not also written with us in mind. In any event, thank you (again) for you contributions. I save dozens of hours weekly here on the forum with so many useful ideas and code offerings in which I can trust.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines