Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-17-2004, 09:08 PM   PM User | #1
doggo18
New Coder

 
Join Date: Apr 2003
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
doggo18 is an unknown quantity at this point
Get first letter from string..

How do I get the first letter of a string? I can't seem to figure it out...

for example: '23V6', I want to get the 'V'

Thanks for reading
doggo18 is offline   Reply With Quote
Old 05-17-2004, 09:19 PM   PM User | #2
l3vi
Regular Coder

 
Join Date: Jan 2003
Posts: 400
Thanks: 0
Thanked 0 Times in 0 Posts
l3vi is an unknown quantity at this point
I was having the same problem yesterday. Heres how:

First, define the function you will use:
PHP Code:
function str_split($string) {
   for (
$x 0$x strlen($string); $x++) {
       
$array[$x] = substr($string$x1);
   }
   return 
$array;

Then, do the code to get the string:
PHP Code:
       $text "23V6";
       
$string str_split($text1);
       
$string $string[3];

       echo 
$string
That should do it. The number in the brackets is the number of the letter you want from the string...
l3vi is offline   Reply With Quote
Old 05-17-2004, 09:20 PM   PM User | #3
doggo18
New Coder

 
Join Date: Apr 2003
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
doggo18 is an unknown quantity at this point
Fast response Thanks!

And now why didn't I think of that..
doggo18 is offline   Reply With Quote
Old 05-17-2004, 11:07 PM   PM User | #4
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
I don't get it.

First off, i don't understand that function at all. It's completely unnescecary, because
PHP Code:
$text='23V45';
$string=$text{3}; 
would give me exacly the same, without needing the function to first turn the string into an array of 1 character elements.

Then i don't understand how all this gets you the first letter.
I mean you here hardcode the position.
In fact, it is wrong, because $string[3] will return '6' because it's a zero based array.

I thought you wanted to dynamically get the first letter from any string, or is it always the third character?

Anyway, the dynamic version would be something like:
PHP Code:
$text "23V6"
for (
$i 0$i strlen($text); $i++) { 
    if (
eregi("[a-z]"$text{$i})) {
       
$string $text{$i};
       break(); 
   }

__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 05-17-2004, 11:29 PM   PM User | #5
doggo18
New Coder

 
Join Date: Apr 2003
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
doggo18 is an unknown quantity at this point
Well I did want to do it dynamically.. but his answer gave me the insight to make the loop myself... I didn't want to bother you lot with it anymore since I could figure it out with the part I got from levi.. but I will ofcourse update it with the better solution you posted raf.. it's way more elegant than mine hehe
doggo18 is offline   Reply With Quote
Old 05-18-2004, 04:07 AM   PM User | #6
l3vi
Regular Coder

 
Join Date: Jan 2003
Posts: 400
Thanks: 0
Thanked 0 Times in 0 Posts
l3vi is an unknown quantity at this point
Oops, forgot to post that that was for older versions of php that dont support the str_split command
l3vi is offline   Reply With Quote
Old 05-18-2004, 03:24 PM   PM User | #7
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
You don't necessarily need to define a new function for this problem:

PHP Code:
preg_match('/^[^a-z]*([a-z])/im'$string$matches); 
Using the code above, the first letter in a string will appear in $matches[1].
__________________
De gustibus non est disputandum.
mordred is offline   Reply With Quote
Old 05-18-2004, 04:09 PM   PM User | #8
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
and for no other reason whatsoever other than to be contrary ...

PHP Code:
<?
echo substr$text,( strspn$text "1234567890" ) +) ,) ; 
?>
ok ok I admit it , it's cos I can't do regex
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:00 PM.


Advertisement
Log in to turn off these ads.