String question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

String question

Post 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"
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Do you just want the first word or all the words before the first instance of is?

Mac
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post 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.
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

found it

substr
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Meteo
Forum Newbie
Posts: 24
Joined: Sun Jan 18, 2004 10:19 am
Contact:

Post 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";
?>
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post by phpcoder »

but if name contain spaces then
like $A=smith john is ..."
coz he wants to extract name
User avatar
Meteo
Forum Newbie
Posts: 24
Joined: Sun Jan 18, 2004 10:19 am
Contact:

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply