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
$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
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 " ", , \t,
and \f
$keywords = preg_split("/ /", "this is string");
print_r($keywords);
?>
Thanks Bishop
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
)
$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);
$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);