SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot virginiamonkeys's Avatar
    Join Date
    Jun 2003
    Location
    Stafford, Virginia, USA
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How Do You Explode an Already Exploded Array?

    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:

    PHP Code:
    $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:

    PHP Code:
    //explode further

    $explodedlist = array();

    function 
    exploder(&$item$key)
    {
        foreach (
    $newlist[$key] as $key => $value)

        {
          
    $explodedlist[$key] = explode(' '$value);
        } 
    }
    array_walk_recursive($newlist'exploder'); 
    Help.

  2. #2
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you post a sample string, the explosive characters and your current results?

  3. #3
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With
    PHP Code:
    $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
    Code:
    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?

  4. #4
    SitePoint Zealot virginiamonkeys's Avatar
    Join Date
    Jun 2003
    Location
    Stafford, Virginia, USA
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:

    //explode at newline
    $lines explode("\n",$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):

    Code:
    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:

    Code:
    NULL

  5. #5
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it vital to have a distinction between "\n" and "<br>"? Why not:
    PHP Code:
    str_replace('<br>'"\n"$array); 
    Then explode by new lines and then spaces?

  6. #6
    SitePoint Zealot virginiamonkeys's Avatar
    Join Date
    Jun 2003
    Location
    Stafford, Virginia, USA
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    $array explode("\n"$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.

  8. #8
    Jewish Juggernaut mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice code man.

  9. #9
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, I said it was dirty :P
    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.

  10. #10
    Jewish Juggernaut mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zarin Denatrose View Post
    Hey, I said it was dirty :P
    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.
    O i wasnt being sarcastic, its hard to tell from a post right.

    If it works it works

  11. #11
    SitePoint Zealot Ripe's Avatar
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How bout a function that can handle unlimited number of explodes!

    PHP Code:
    <?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..

  12. #12
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by virginiamonkeys View Post
    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.
    Can you give a real sample of your input? Not literally but something that can better represent the data you're working on.
    Imagination is more important than knowledge. - Einstein

  13. #13
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •