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!
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.
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
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.
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.
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.
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.
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...