A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearbreak a word in letters php - page 1
User InfoPosts
break a word in letters php#1
I want to accept a string from a form and then break it into an array of characters using PHP, for example:

$a = (professor(;
$b[0] == (p(;
$b[1] == (r(;
$b[2] == (o(;
.
.
.
.
.
$b[8] = (r(;

posted date: 2009-04-10 06:17:00


Re: break a word in letters php#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-10 06:17:01


Re: break a word in letters php#3
You don(t need to do that. In PHP you can access your characters directly from the string as if it where an array:$var = "My String";echo $var[1]; // Will print "y".

posted date: 2009-04-10 06:23:00


Re: break a word in letters php#4
I didn't know that. good tip.

posted date: 2009-04-10 06:25:00


Re: break a word in letters php#5
str_split($word);This is faster than accessing $word as an array. (And also better in that you can iterate through it with foreach().) Documentation.

posted date: 2009-04-10 06:26:00


Re: break a word in letters php#6
Why is it faster? It just returns an array.

posted date: 2009-04-10 10:10:00


Re: break a word in letters php#7
It is not faster; in fact, it's slower - it has to create an additional array and see where to split the original string depending on the second parameter.

posted date: 2009-04-11 05:56:00


Re: break a word in letters php#8
Hey, I saw your profile and I can see you didn't mark any answer as correct for any of the questions you made. When you find your solution, you should mark that as the correct answer for your question. That will help anyone coming here with the same problem to see what the solution actually is.

posted date: 2009-04-11 06:02:00


Re: break a word in letters php#9
You're correct: a benchmark confirms that this is 50% slower than your method.

posted date: 2009-04-11 06:32:00


Re: break a word in letters php#10
Be careful because the examples above only work if you are treating ASCII (single byte) strings.

posted date: 2009-04-11 14:09:00


Re: break a word in letters php#11
FYI, $var{1} will work, but it's being deprecated as of PHP6 in favor of $var[1].

posted date: 2009-04-11 14:18:00


Re: break a word in letters php#12
If you really want the individual characters in a variable of array type, as opposed to just needing to access the character by index, use:$b = str_split($a)Otherwise, just use $a[0], $a[1], etc...

posted date: 2009-04-11 14:44:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.