Php for loop array not working -
i'll post, hello, world
expecting
hello,6 h e l l o , world5 w o r l d
instead get
hello,6 h e l l o , world5
there 2nd word not spell?
$name = $_post['engname']; $convert = array_combine($letters, $jap); function get_num_of_names($name) { $name = explode(" ", $name); $name_mainlen = count($name); for($i=0; $i <= $name_mainlen + 1; $i++) { echo $name[$i]; $name[$i] = str_split($name[$i]); $namelen = count($name[$i]); echo $namelen . '<br/>'; function spellname($x, $namelen) { for($i=0; $i <= $namelen; $i++) { echo $x[$i] . '<br/>'; } } spellname($name[$i], $namelen); } } get_num_of_names($name);
you're defining function in loop. script should implode function 'spellname' defined
error, if you'd activate error reporting.
a function defined when encountered. if place in loop, defined on each iteration. since can't define 2 functions same name, script aborts fatal error.
apart that, there easier ways want do:
$str = 'hello world'; $words = explode(' ', $str); $output = array(); foreach ($words $word) { $output[] = join('<br />', str_split($word)); } echo join('<br /><br />', $output);
or, one-liner:
echo join('<br /><br />', array_map(create_function('$a', 'return join("<br />", str_split($a));'), explode(' ', $str)));
or, shorter different approach (regards @long ears):
echo join('<br />', str_replace(' ', '', str_split($str)));
Comments
Post a Comment