Current location: Hot Scripts Forums » Programming Languages » PHP » convert each character in string to array


convert each character in string to array

Closed Thread
  #1 (permalink)  
Old 01-30-06, 02:40 PM
robbydweb robbydweb is offline
Newbie Coder
 
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
convert each character in string to array

Anyone know the best way to convert all the characters in a string into an array

for example
PHP Code:

$string "this is string";


// then some how explode each character including spaces
$data explode($string);

//so result would be something like:
$data[0] = 't';
$data[1] = 'h';
$data[2] = 'i';
$data[4] = ' '
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #2 (permalink)  
Old 01-30-06, 03:25 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
This should do the trick

PHP Code:

$string "this is string";


$nr 0;

while (isset(
$string{$nr})) {

    
$data[$nr] = $string{$nr};
    
    
$nr++;
    

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #3 (permalink)  
Old 01-30-06, 03:30 PM
Klesti Klesti is offline
Newbie Coder
 
Join Date: May 2004
Location: Albania
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Note that you can access the string characters also with this syntax, using [] instead of {}, anyway if you want to have array functionality you can do it in that way.

PHP Code:

$s "this is a string";

echo 
$s[2]; 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #4 (permalink)  
Old 01-30-06, 03:43 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
In PHP a string is actually an array, so $foo ="hotscripts", you could access the "h" with $foo[0], the "o" with $foo[1], and so on.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #5 (permalink)  
Old 01-30-06, 06:36 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by End User
In PHP a string is actually an array, so $foo ="hotscripts", you could access the "h" with $foo[0], the "o" with $foo[1], and so on.
I think that's done with curly braces as opposed to square brackets. So in your example: $foo{1} == "o".

Quoted from http://www.php.net/substr:
PHP Code:

<?php


// Accessing single characters in a string
// can also be achived using "curly braces"
$string 'abcdef';
echo 
$string{0};                 // a
echo $string{3};                 // d
echo $string{strlen($string)-1}; // f

?>

Last edited by Keith; 01-30-06 at 06:42 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #6 (permalink)  
Old 01-30-06, 09:05 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Both [] and {} work. However, if you include the {} form in a quoted string, the results are not as expected -
PHP Code:

<?php

$string 
"abcdef";
echo 
"The 1st char of the string is: $string[0]<br>";
echo 
"The 1st char of the string is: $string{0}<br>";
?>
Results in this output -
The 1st char of the string is: a
The 1st char of the string is: abcdef{0}
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 01-30-06 at 09:09 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #7 (permalink)  
Old 01-30-06, 09:41 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Ah, good point.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #8 (permalink)  
Old 01-30-06, 09:53 PM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by mab
Both [] and {} work. However, if you include the {} form in a quoted string, the results are not as expected -
PHP Code:

<?php

$string 
"abcdef";
echo 
"The 1st char of the string is: $string[0]<br>";
echo 
"The 1st char of the string is: $string{0}<br>";
?>
Results in this output -
The 1st char of the string is: a
The 1st char of the string is: abcdef{0}
Or if you always use {} for strings, etc, then you don't have the problem:
PHP Code:

$string "abcdef";

echo 
"The 1st char of the string is: $string[0]<br>";
echo 
"The 1st char of the string is: {$string{0}}<br>";

// plus benefits
echo "The string is: $stringg<br>";
echo 
"The string is: {$string}g<br>";
?> 

Last edited by Patiek; 01-30-06 at 09:59 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #9 (permalink)  
Old 06-20-09, 12:16 PM
mwafi mwafi is offline
New Member
 
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
use this
PHP Code:

$data=str_split($string); 

$data is an array
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #10 (permalink)  
Old 06-21-09, 12:36 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
A three and a half year old topic dude. Really?
__________________
The toxic ZCE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Closed Thread

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wanted to replace special characters in MS Access using VB, but more than 1 character cebuy Visual Basic 3 12-04-05 06:30 PM
Return String values from a object saved in an array mr_wazzup Everything Java 1 04-15-05 10:11 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM


All times are GMT -5. The time now is 01:59 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.