x10Hosting Forums

Corporate Free Hosting for the Masses.



Register

Reply
 
LinkBack Thread Tools Display Modes
x10Hosting Member

Join Date: Apr 2008
Posts: 34
Credits: 408
radofeya is on a distinguished road
Quote  
09-29-2008, 07:16 AM
Split string

Can I split a string word by word into an array by using PHP?

example:

$string = "hello";
then, split string into "h", "e", "l", "l", "o".

thanks
Reply With Quote
radofeya is offlineReport Post
x10 Sophmore

Scoochi2's Avatar

Join Date: Aug 2008
Posts: 118
Credits: 1,277
Scoochi2 will become famous soon enough
Location: Southport!

Quote  
09-29-2008, 11:45 AM
Re: Split string

There's a function just for that
PHP Code:
$string 'Hello there!';
$array str_split($string);
print_r($array); 
The above will output:
Code:
Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => t
    [7] => h
    [8] => e
    [9] => r
    [10] => e
    [11] => !
)
So the function you need is str_split. You can also choose to split the string every 2 letters, 3 letters, or whatever you desire, but the default will split every letter into it's own array key.
View PHP.net for more info.
__________________
If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending...
Reply With Quote
Scoochi2 is offlineReport Post
x10Hosting Member

Join Date: May 2008
Posts: 53
Credits: 405
scopey is on a distinguished road
Quote  
09-29-2008, 10:11 PM
Re: Split string

Similarly, you can just use explode() if you split by null:

PHP Code:
// returns an array with each character:
explode("Hello there!"""); 
__________________
- When in doubt, refer to the PHP manual.
Reply With Quote
scopey is offlineReport Post
x10Hosting Member

Join Date: Apr 2008
Posts: 34
Credits: 408
radofeya is on a distinguished road
Quote  
10-02-2008, 03:35 AM
Re: Split string

Waw, that is great. Thanks.
Reply With Quote
radofeya is offlineReport Post
Reply

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Word Scrambler - String Questions masterjake Programming Help 2 09-17-2008 04:51 PM
Multibyte String Functions sprocket Free Hosting 1 08-13-2008 05:36 AM
split account? Matt Free Hosting 11 06-29-2005 02:08 AM


All times are GMT -5. The time now is 06:04 PM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

Loans | Mortgage | Personal Car Finance | Loans | Free Advertising