String question
Moderator: General Moderators
-
Shendemiar
- Forum Contributor
- Posts: 404
- Joined: Thu Jan 08, 2004 8:28 am
String question
$A = "Kalle is dead"
$A = "Mikaela is alive"
with what function do i get (return string until 'is' and strip off the last space)
$A="Kalle"
$A="Mikaela"
$A = "Mikaela is alive"
with what function do i get (return string until 'is' and strip off the last space)
$A="Kalle"
$A="Mikaela"
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
-
Shendemiar
- Forum Contributor
- Posts: 404
- Joined: Thu Jan 08, 2004 8:28 am
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
substr is very useful that way, I'm not 100% sure how substr may work because it would be difficult to find the length of the name when it's only part of a string
Code: Select all
<?php
$A = "Kalle is dead";
$B = "Mikaela is alive"; /* this variable name should be different from the above one, they were both $A */
$A_exploded = explode(" ", $A);
$B_exploded = explode(" ", $B);
$A_name = $A_exploded[0];
$B_name = $B_exploded[0];
echo "$A_name<br>$B_name";
?>I would suggest keeping it repetitive, so if that were the case, then just use the first two keys of the array
But if not, an idea could be to have only the names start with uppercased letters, then there could be a script testing to see if the first letter is capitalized, and if true, add the word to the variable
But if not, an idea could be to have only the names start with uppercased letters, then there could be a script testing to see if the first letter is capitalized, and if true, add the word to the variable
What about:
$a = "Kalle is dead";
$a_exploded = explode(" is ",$a);
Then, $a_exploded[0] would be everything before " is ", in this case the name, and $a_exploded[1] would be the status of the person.
$a = "Kalle is dead";
$a_exploded = explode(" is ",$a);
Then, $a_exploded[0] would be everything before " is ", in this case the name, and $a_exploded[1] would be the status of the person.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.