0

I want to convert strings to array without using explode function in php I want output something like this ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) but without using explode().

    <?php

     $str="this is string"; 

    ?>

should be like this arr[0]=this arr[1]=is arr[2]=string

2
  • 3
    What is the pattern of the string, and why without explode, what's wrong with it ? Commented Aug 14, 2014 at 7:39
  • 1
    where is the effort ? Do your home work urself, if problem raise, share here .. Commented Aug 14, 2014 at 7:40

7 Answers 7

0
$j = mb_strlen($theString);
for ($k = 0; $k < $j; $k++) 
{
    $char = mb_substr($theString, $k, 1);
    $var_arr[$k] =  $char;
}

The above code don't use any pattern to split the string.

It ttakes one character at a time

EDIT suppose you have string

$s = 12.3.4.09.20

it will give the array as

array = ('1','2','.','3','.','4','.','0','9','.','2','0');

EDIT : COMPLETE CODE

<?php
$theString = "1.2.34.87";
$var_arr = array();
$j = mb_strlen($theString);
for ($k = 0; $k < $j; $k++) 
{
    $char = mb_substr($theString, $k, 1);
    $var_arr[$k] =  $char;
}
print_r($var_arr);
?>

go to http://phpfiddle.org/ and test over there

check the images as a proof enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

can you tell what will be its output? I am getting errors
i didnt get your code..so am asking to show me the complete program
its diff from what i want...my code:::$str = "1,2,3,4,5"; o/p should be this: ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) ...
0

I hope your pattern is like below "this is string" hence following code can be used for same:

<?php
// split the phrase by any number of commas or space characters,
// which include " ", \r, \t, \n and \f
$keywords = preg_split("/ /", "this is string");
print_r($keywords);
?>

Thanks Bishop

Comments

-1

My solution for this...

$string = 'This-is-the-string';
$word = '';$warray = array();

for($i=0; $i<strlen($string);$i++){
    if($string[$i]=='-'){$warray[] = $word;$word = '';}
    else $word .= $string[$i];
}

if($word!='')$warray[] = $word;//Last word;

echo "<pre>";print_r($warray);die;

Output

Array
(
    [0] => This
    [1] => is
    [2] => the
    [3] => string
)

Comments

-1
    $theString = "this is string";
    $var_arr = array();
    $j = mb_strlen($theString);
    $chars = "";
    for ($k = 0; $k < $j; $k++) 
    {

        $char = mb_substr($theString, $k, 1);
        if($char == " ")
        {
            $var_arr[] =  $chars;
            $chars = "";
        }
        else{
            $chars .= $char;
        }

        if( ($k + 1) == $j)
        {
            $var_arr[] =  $chars;
        }

    }
    print_r($var_arr);

Comments

-1
$var = 'Rahul,Rohit,Sumeet,Abhi'; //your string 
$len = strlen($var);
$glue = ','; // place your glue here
$j=0;
for($i=0; $i<= $len;$i++){
    $arr[$j] .= trim($var[$i],$glue);   
    if($var[$i] == $glue){
        $j++;   
        continue;
    }
    else{

    }
}

print_r($arr);

Comments

-1
<?php
 $str  = "This is a string";
 $a =array();
 $word = "";
    for($i=0;$i<strlen($str);$i++){
      if($str[$i] == " "){
         $a[] = $word;
         $word = "";
      } else{
         $word .= $str[$i];
      }
    }

// for last word
if($word != ''){
    $a[] = $word;
}

print_r($a);
?>

Output: Array ( [0] => This [1] => is [2] => a [3] => string )

Comments

-1
$string = "imran,arsh,feroz,rushi";
$i = 0;
$word = '';
$temparray = [];

while(!empty($string[$i]))
{
    if($string[$i] == ','){
        $temparray[] = $word;
        $word = '';
    }else{
        $word .= $string[$i];
    }
    $i++;
}

if(!empty($word))
{
    $temparray[] = $word;
}
 
print_r($temparray);
output 
Array
(
    [0] => imran
    [1] => arsh
    [2] => feroz
    [3] => rushi
)

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.