Reputation: 555
I'm pretty new to webcoding and would like to improve a bit in php, but I'm already stuck :/
What I am trying to do is to have a sentence like
"Simon likes apples"
Now I have an input form for the name so I can choose the name. So it looks like
"$name likes apples"
<table>
<tr>
<td><input type="text" id="name1" name="name1"></td>
<td><input type="submit" id="submit" name="submit" value="Apply"></td>
</tr>
</table>
</div>
<div id="text">
<?php
if(isset($_REQUEST['submit'])) {
$name1 = $_REQUEST['name1'];
echo "$name1 likes apples.";
}
?>
</div>
</form>
Everything fine till now. Whatever name I type in replaces $name. Now what I want to do is to change the "likes" to "like" whenever i type in a pronoun (I, You, They etc.), which makes sense obviously. Now here is where I don't know what to do. My current code (which doesnt work) looks like this:
<?php
if(isset($_REQUEST['submit'])) {
$name1 = $_REQUEST['name1'];
if ($name1 = "I", "You") {
$verb = "like";
}
else {
$verb = "likes";
}
echo "$name1 $verb apples";
}
?>
Also is there a way to make it case insensitive?
Upvotes: 2
Views: 1103
Reputation: 6526
Case insensitive replacement, $lut
table defines what should be replaced with what. You may place it inside the replaceMe
function if you don't want to change it over time.
Code:
<?php
$lut = [
'like' => [
'I',
'You',
'They',
'We',
],
'likes' => [
'she',
'he',
'it',
'someone',
'somebody',
],
];
function replaceMe(string $name, array $lut) : string
{
foreach ($lut as $key => $value) {
$nameLower = strtolower($name);
$valueLowerArr = array_map(
function($input) {
return strtolower($input);
},
$value
);
if (in_array($nameLower, $valueLowerArr)) {
return strtolower($key);
}
}
return '';
}
$name = 'She';
echo "$name = " . replaceMe($name, $lut) . '<br>' . PHP_EOL;
$name = 'I';
echo "$name = " . replaceMe($name, $lut) . '<br>' . PHP_EOL;
$name = 'iT';
echo "$name = " . replaceMe($name, $lut) . '<br>' . PHP_EOL;
$name = 'TheY';
echo "$name = " . replaceMe($name, $lut) . '<br>' . PHP_EOL;
$name = 'nobody';
echo "$name = " . replaceMe($name, $lut) . '<br>' . PHP_EOL;
Gives Result:
She = likes<br>
I = like<br>
iT = likes<br>
TheY = like<br>
nobody = <br>
Upvotes: 0
Reputation: 454
there are few problems in your code .
single =
means SET my variable to whatever string is after . so if you want to see if your variable is equal to a string like You
, you have to use ==
, it will return true or false .
now we want to say if my variable was You
or I
, change my second variable to like . thats gonna be the output :
if ($name1 == "I" || $name1 == "You") {
$verb = "like";
}
else {
$verb = "likes";
}
so now it says if $name1
was equal to I OR(||
) $name1
was equal to You
, change the $verb
to like .
we use ||
as OR and &&
as AND.
if you want echo variables and string you should use .
as split between your variable and string . so it will be like this :
echo $name1 . $verb . "apples";
it's kind of like +
but not in a math way , it just means add .
UPDATE
yes . there is a way to check your string case insensitive . you have to use strcasecmp() .
in your code , it should be like :
if (strcasecmp($name1,"I") == 0 || strcasecmp($name1,"You") == 0 ) {
$verb = "like";
}
else {
$verb = "likes";
}
Upvotes: 2