preg match with multiple spaces ?

Discussion in 'PHP' started by thesurface, Dec 22, 2013.

  1. #1
    hello anyone know how to preg_match data with multiple spaces like this one ?

     
        [12] => 200- [ 1]  Te           Test
        [13] => 200- [ 2]  Tes          Test2
        [14] => 200- [30]  Test         Test3
    
    Code (markup):
    I need to get Test, Test2 and Test3 thx!
     
    thesurface, Dec 22, 2013 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Taking what you pasted, and creating an array:
    
    <?php
       $array = array(
               12 => '200- [ 1]  Te  Test',
               13 => '200- [ 2]  Tes  Test2',
               14 => '200- [30]  Test  Test3'
         );
    
       foreach($array as $key => $value) {
         $value = array_reverse(explode(' ', preg_replace('!\s+!', ' ', $value)));
         echo $value[0].'<br>';
       }
    ?>
    
    PHP:
    It takes the value in the array, removes excess whitespace, splits the values on single space afterwards, reverses the array to get the last bit, and then echo's that value.
     
    PoPSiCLe, Dec 22, 2013 IP
  3. thesurface

    thesurface Active Member

    Messages:
    260
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #3
    Thanks but i need to parse that as text so its like $data = "that multiple spaces etc ...";

    I dont havy any array just simple text to parse
     
    thesurface, Dec 22, 2013 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    So? Just rewrite it without using an array then?
    
    <?php
       $var =   "[12] => 200- [ 1]  Te  Test
      [13] => 200- [ 2]  Tes  Test2
      [14] => 200- [30]  Test  Test3";
    
      $array = explode("\r\n",$var);
      foreach($array as $key => $value) {
         $value = array_reverse(explode(' ', preg_replace('!\s+!', ' ', $value)));
         echo $value[0].'<br>';
       }
    ?>
    
    PHP:
    This takes the values, given that they actually consist of a format like you provided (splits on \r\n which is lineshifts) and makes an array out of it which is then parsed the same way as the original array.
     
    PoPSiCLe, Dec 22, 2013 IP
  5. thesurface

    thesurface Active Member

    Messages:
    260
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #5
    Thanks for help but i need to get Test, Test2 and Test3 and in your $var there isnt that multiple spaces like i posted in first post.
     
    thesurface, Dec 22, 2013 IP
  6. jk.deff

    jk.deff Active Member

    Messages:
    59
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    55
    #6
    See version of the code below:
    $text = "200- [ 1]  Te           Test";
    
    echo get_result($text);
    
    function get_result($text)
    {
        $ret = '';
        if( preg_match_all('/[^\s]+/',$text, $matches) )
        {
            $ret = $matches[0][count($matches[0])-1];
        }
    
        return $ret;
    }
    Code (markup):
    If this is not what you expected, please write some examples of input data, and what you need to obtain in result.
     
    jk.deff, Dec 22, 2013 IP
  7. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #7
    If you can't take someones example code and tweak it to your liken how the hell were you planning to finish your own script?

    I swear people are either lazy or ask meaningless questions they don't really need to know the answer to.
     
    NetStar, Dec 22, 2013 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    Really? It's called copying, mate - the text I used in the demonstration was the exact same text you posted in your first post. Why don't you just LOOK AT THE PROVIDED CODE, or better, copy it and test it yourself, and see that it works. The code provided outputs
    Test
    Test2
    Test3
    If you need it on one line with comma as a separator, just change the output values...
     
    PoPSiCLe, Dec 24, 2013 IP