PDA

View Full Version : [Q] PHP->How to implode/explode without delimiter?


fugitive123
01-13-2006, 03:04 AM
Can anybody tells me whether there is a built-in function to perform splitting or joining of arrays without using delimiter? :confused: Please don't give me alternative solution by ways of custom function. I know how to do it with my own function but I would like to know if there is already a PHP built-in function to cater for this scenario. TQ.

Flash Gordon
01-13-2006, 03:06 AM
strstr
preg_match

fugitive123
01-13-2006, 03:43 AM
Thanks. Will try to understand what these built-in functions you recommended will do. Actually I want to use simple solution to convert from string to array and array to string without having to manually split or join them. Built-in functions like explode and implode require only two lines of coding.

Flash Gordon
01-13-2006, 03:52 AM
Q = ("foobar").split("");
trace(Q);

fugitive123
01-13-2006, 04:40 AM
But the "split" example you gave above and "join" function are in ActionScript which accepts null delimiter. It's a different case in PHP which does not allow null delimiter and I end up writing my own function.

Flash Gordon
01-13-2006, 04:46 AM
Perhaps if you knew why you are so attiment on not having a delimeter of create custom functions, we could help better.

Otherwise, just use the delimeter. It can't be that much of a hassle.

fugitive123
01-13-2006, 05:17 AM
Actually, I'm trying to break up a long sentence and store each character into an array and perform some checking and manipulation, and then join them back again into a manipulated sentence without having to write a custom function to perform the conversion prior to and after the checking. I need to have a ready array with the each character of the Text being splitted into respective slot. before performing the necessary check. Without delimiter, I can't use the one line of "explode" and "implode" function . By using another custom function, the performance speed may be affected if the text is too big. Hope this helps to explain my concern.

I've been always trying to improve performance speed and reduce bandwidth cost and find any possible solution if applicable, use the already built-in function, otherwise, write my own custom function. BTW, thanks for providing the two built-in functions. Will look into them.

Flash Gordon
01-13-2006, 06:06 AM
What you want can't be done. Here is a simple enough function to manipulate:


<?php

function myFunc($str) {
global $myArray;
$i = 0;

while(strlen($str) > $i) {
$myArray[] = substr($str, $i, 1);
echo $myArray[$i] . "<br>";
$i++;
}

}
$ok = "Hello World";
myFunc($ok);

?>
Output:

H
e
l
l
o

W
o
r
l
d

fugitive123
01-13-2006, 06:45 AM
Thanks Flash. As I mentioned, I'm not asking for anybody to provide me a custom function. I myself had written my own custom function using a for loop, which used the strlen and substr like what you did above. Still, my main concern is the performance speed.....

Flash Gordon
01-13-2006, 07:48 AM
there will be no performance hit with that code, I'm quite sure. Built-in functions do the same things.

Xeef
01-13-2006, 04:11 PM
try



<?php
$A="Hallo";
$B=chunk_split ($A,1,",");
$B=explode(",",$B);
printf ($B);
?>

not sure abouth the use of "chunk_split" and have no time jet
so the above isn't totaly corect ! but something like