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]
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
...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.
Copy link to clipboard
Copied
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.