-1

how i can explode string into an array. Acctually i want to translate english language into the braille. First thing i need to do is to get the character one by one from a string,then convert them by mathing the char from value in database and display the braille code using the pic. As example when user enter "abc ef", this will create value separately between each other.

Array
(
[0] => a
[1] => b
[2] => c
[3] => 
[4] => e
[5] => f
)

i tried to do but not get the desired output. This code separated entire string.

$papar = preg_split('/[\s]+/', $data);
print_r($papar);

I'm sorry for simple question,and if you all have an idea how i should do to translate it, feel free to help. :)

Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40
  • 1
    Maybe try str_split http://stackoverflow.com/questions/9814375/php-explode-all-characters – Lancetarn Nov 26 '13 at 04:11
  • I've updated with an answer that addresses both PHP5 and PHP4. – zeantsoi Nov 26 '13 at 04:37
  • isnt string already an array of characters? you can do for($ctr=0; $ctr < strlen($string); $ctr++) { echo $string{$ctr}; } – Ramz Nov 26 '13 at 05:11
  • Why would you accept an answer that returns a series of strings, when you have _specifically_ cited an _array_ as being your desired return value? – zeantsoi Nov 26 '13 at 05:49

3 Answers3

2

If you're using PHP5, str_split will do precisely what you're seeking to accomplish. It splits each character in a string – including spaces – into an individual element, then returns an array of those elements:

$array = str_split("abc ef");
print_r($array);

Outputs the following:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => 
    [4] => e
    [5] => f
)

UPDATE:

The PHP4 solution is to use preg_split. Pass a double forward slash as the matching pattern to delimit the split character-by-character. Then, set the PREG_SPLIT_NO_EMPTY flag to ensure that no empty pieces will be returned at the start and end of the string:

$array = preg_split('//', "abc ef", -1, PREG_SPLIT_NO_EMPTY); // Returns same array as above
zeantsoi
  • 25,857
  • 7
  • 69
  • 61
  • voilla,,looks great,. But how i can check the array value is exist or not. i already modified my code, but cant post in here because my reputation is less than 10. So far, i can split my string into array, but how can i display the white space as an ouput. i make all array loop over each char using for statement and using if statement to assign "&nbsp" for empty array value like this.. if($papar[$a]=="") { echo ' '; } – Norlihazmey Ghazali Nov 26 '13 at 04:51
  • If this answer helped address your original question (which, based on your desired output, I would contend it did), you should accept it as correct. Then, you should ask this follow-up question separately so that you aren't asking new questions in the comments of an answer. IMO, it's very difficult to read code when it's contained within comments. – zeantsoi Nov 26 '13 at 04:56
  • thank you zeantsoi, i already post my code – Norlihazmey Ghazali Nov 26 '13 at 05:01
0

PHP has a very simple method of getting the character at a specific index.

To do this, utilize the substring method and pass in the index you are looking for.

How I would approach your problem is

  1. Ensure the string you are converting has at least a size of one
  2. Determine how long the string is

    int strlen ( string $string )

  3. Loop over each character and do some operation depending on that character

With a string "abcdef"

$rest = substr("abcdef", -1); // returns "f"

See the substr page for a full example and more information

http://us2.php.net/substr

Greg Hilston
  • 2,397
  • 2
  • 24
  • 35
-1

You can iterate the string itself.

$data = 'something string';

for($ctr = 0; $ctr < strlen($data); $ctr++)
{
    if($data{$ctr}):
        $kk = mysql_query("select braille_teks from braille where id_huruf = '$data{$ctr}'");
        $jj = mysql_fetch_array($kk);
        $gambar = $jj['braille_teks'];
    ?>
    <img src="images/<?php echo  $gambar;?>">
    <?php
     else:
       echo "&nbsp";
}
Ramz
  • 326
  • 2
  • 6
  • This does not return an array as the asker has specified. – zeantsoi Nov 26 '13 at 05:48
  • @zeantsoi but this is a valiable alternative.. see http://stackoverflow.com/help/how-to-answer – Ramz Nov 26 '13 at 09:10
  • How is this an alternative when the question cites an array, but this answer returns a string? I don't see this as being an alternative at all. – zeantsoi Nov 26 '13 at 09:17
  • its alternative.. i just given him an iteration without converting string to array. and oh! by the way, strings are treated/considered as array of char elements :) – Ramz Nov 26 '13 at 09:23