Page 1 of 1

String question

Posted: Mon Jan 19, 2004 9:32 am
by Shendemiar
$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"

Posted: Mon Jan 19, 2004 9:40 am
by twigletmac
Do you just want the first word or all the words before the first instance of is?

Mac

Posted: Mon Jan 19, 2004 9:43 am
by Shendemiar
Before ' is ' cos the names can be multi-part, i think.

But i think i can solve this. Just say the name of the function that gives x first chars of string.

Posted: Mon Jan 19, 2004 9:47 am
by Shendemiar
found it

substr

Posted: Mon Jan 19, 2004 10:16 am
by twigletmac
I assumed that the name would be variable length - substr() seems more useful if it'll always be the same length - or is it that the following data is always the same length?

Mac

Posted: Mon Jan 19, 2004 3:26 pm
by Meteo
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";
?>

Posted: Mon Jan 19, 2004 3:39 pm
by phpcoder
but if name contain spaces then
like $A=smith john is ..."
coz he wants to extract name

Posted: Mon Jan 19, 2004 4:34 pm
by Meteo
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

Posted: Mon Jan 19, 2004 6:18 pm
by pickle
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.