I have an array that I have exploded. I’m trying to explode that exploded one further but am having trouble.
This is the code that I got to explode it the 1st time:
$newlist = array();
foreach ($array as $key => $value)
{
$newlist[$key] = explode('<br>', $value);
}
Now I want to have this $newlist to explode. I’ve tried doing a few things and am having trouble. This is the latest attempt:
//explode further
$explodedlist = array();
function exploder(&$item, $key)
{
foreach ($newlist[$key] as $key => $value)
{
$explodedlist[$key] = explode(' ', $value);
}
}
array_walk_recursive($newlist, 'exploder');
Help.
Could you post a sample string, the explosive characters and your current results?
With
$str = "12:34:56|78:91:23|45:67:89";
$array = array();
$array = explode('|', $str);
foreach($array as $key => $str)
{
$array[$key] = explode(':', $str);
}
echo "<pre>";
print_r($array);
I get the results
Array
(
[0] => Array
(
[0] => 12
[1] => 34
[2] => 56
)
[1] => Array
(
[0] => 78
[1] => 91
[2] => 23
)
[2] => Array
(
[0] => 45
[1] => 67
[2] => 89
)
)
Is that along the lines of what you need?
Actually, looking at my code I realize that I’ve already exploded it twice and want to explode it a third time. Here’s my code:
//explode at newline
$lines = explode("\
",$array);
//explode at <br>
$newlist = array();
foreach ($lines as $key => $value)
{
$newlist[$key] = explode('<br>', $value);
}
//explode at spaces
$explodedlist = array();
function exploder(&$item, $key)
{
foreach ($newlist[$key] as $key => $value)
{
$explodedlist[$key] = explode(' ', $value);
}
}
array_walk_recursive($newlist, 'exploder');
What I get with the 1st 2 explosions is this (this is a slightly edited result of my test array):
array(4) {
[0]=>
array(7) {
[0]=>
string(xx) "sample *catchword* sample"
[1]=>
string(xx) "test *catchword*"
[2]=>
string(xx) "text here catchword *catchword* text here"
[3]=>
string(xx) "just regular text"
[4]=>
string(xx) "*catchword* and text"
[5]=>
string(xx) ""
[6]=>
string(xx) "more *catchword* text"
[1]=>
array(7) {
[0]=>
string(xx) "sample *catchword* sample"
[1]=>
string(xx) "test *catchword*"
[2]=>
string(xx) "text here catchword *catchword* text here"
[3]=>
string(xx) "just regular text"
[4]=>
string(xx) "*catchword* and text"
[5]=>
string(xx) ""
[6]=>
string(xx) "more *catchword* text"
[2]=>
array(7) {
[0]=>
string(xx) "sample *catchword* sample"
[1]=>
string(xx) "test *catchword*"
[2]=>
string(xx) "text here catchword *catchword* text here"
[3]=>
string(xx) "just regular text"
[4]=>
string(xx) "*catchword* and text"
[5]=>
string(xx) ""
[6]=>
string(xx) "more *catchword* text"
[3]=>
array(7) {
[0]=>
string(xx) "sample *catchword* sample"
[1]=>
string(xx) "test *catchword*"
[2]=>
string(xx) "text here catchword *catchword* text here"
[3]=>
string(xx) "just regular text"
[4]=>
string(xx) "*catchword* and text"
[5]=>
string(xx) ""
[6]=>
string(xx) "more *catchword* text"
What I get when I run my 3rd explosion is this:
NULL
Is it vital to have a distinction between "
" and “<br>”? Why not:
str_replace('<br>', "\
", $array);
Then explode by new lines and then spaces?
Yes, I need the split at newline and a split at br. This is to organize the results into paragraphs (sort of), then into sentences (sort of). Then I want to split each word of each sentence.
This is a rather dirty way, but it works. You might want to take some time to look it over and find a way to clean it up, but for now here’s what I have:
$array = explode("\
", $str);
$key = 0;
while(isset($array[$key]))
{
$array[$key] = explode("<br>", $array[$key]);
$subkey = 0;
while(isset($array[$key][$subkey]))
{
$array[$key][$subkey] = explode(" ", $array[$key][$subkey]);
if(is_array($array[$key][$subkey]))
{
foreach($array[$key][$subkey] as $subsubkey => $val)
{
if(!trim($val))
{unset($array[$key][$subkey][$subsubkey]);}
}
}
$subkey++;
}
if(empty($array[$key][$subkey]))
{
unset($array[$key][$subkey]);
}
$key++;
if(empty($array[$key]))
{
unset($array[$key]);
}
}
It also takes care of empty words, and then empty arrays, so whitespace shouldn’t cause huge empty problems.
Hey, I said it was dirty
Of course if I were intending on actually using that functionality I’d spend more time on the problem and write a cleaner solution. I was just trying to get working code to the poster.
mkoenig
October 28, 2009, 3:14pm
10
O i wasnt being sarcastic, its hard to tell from a post right.
If it works it works
Ripe
October 30, 2009, 6:57am
11
How bout a function that can handle unlimited number of explodes!
<?php
function multi_explode($delimiters = array(), $string = '')
{
$result = array();
foreach ($delimiters as $delimiter)
if (!isset($array))
$array = explode($delimiter, $string);
else
foreach ($array as $key => $string)
$result[] = explode($delimiter, $string);
return $result;
}
$string = 'Hello<br/>
This is a test!<br/>
Explode this HELLO:THIS:IS:A:TEST<br/>';
$delimiters = array('<br/>', ':');
echo '<pre>';
print_r(multi_explode($delimiters, $string));
?>
Remember to keep the delimiters array in the order you want it exploded.
EDIT: Should of tested this better…
Iimitk
October 31, 2009, 11:08am
12
Can you give a real sample of your input? Not literally but something that can better represent the data you’re working on.
Hi Friends,
I am new to the forum.My name is roshini. This forum is a good place to start.
If anyone has any pointers on what I should be doing first I am open to all suggestions.Best of luck to everyone.