 |
|
|
04-12-2009, 11:00 AM
|
Looping
|
Posts: 468
|
Is it possible to loop through a variable getting whats inside the [*] every time?? (Its not an array).
PHP Code:
$text = "['PLAYER', '12', '-9.5', '140.5', 'Florentino Skears']['PLAYER', '22', '280.5', '160.5', 'Les Lazzo']['PLAYER', '11', '280.5', '120.5', 'Alberto Evan']['PLAYER', '5', '87.2', '215.5', 'Beau Jerome']['PLAYER', '1', '87.2', '140.5', 'Lewis Evan']['PLAYER', '10', '87.2', '65.5', 'Donny Cameron']['PLAYER', '15', '183.9', '230.5', 'Lavern Cable']['PLAYER', '13', '183.9', '170.5', 'Lyle Kozluk']['PLAYER', '17', '183.9', '110.5', 'Bret Ehret']['PLAYER', '26', '183.9', '50.5', 'Shelton White']['PLAYER', '30', '280.6', '215.5', 'Eloy Milne']['PLAYER', '19', '570.5', '140.5', 'Domenic Slater']['PLAYER', '3', '280.4', '190.5', 'Bryce Konstantopoulos']['PLAYER', '29', '280.4', '90.5', 'Benito Basso']['PLAYER', '4', '473.8', '230.5', 'Byran Colin']['PLAYER', '20', '473.8', '170.5', 'Jared Naysmith']['PLAYER', '27', '473.8', '110.5', 'John Pinney']['PLAYER', '16', '473.8', '50.5', 'Deangelo Pugh']['PLAYER', '14', '377.1', '230.5', 'Aurelio Munshower']['PLAYER', '6', '377.1', '170.5', 'Bradley Chauvin']['PLAYER', '21', '377.1', '110.5', 'Darren Wylie']['PLAYER', '18', '377.1', '50.5', 'Will Harris']"
I want to be able to get whats in the first set of [] and then move onto the next set of [] etc...
Last edited by evans123; 04-12-2009 at 11:05 AM..
|
|
|
|
04-12-2009, 11:19 AM
|
Re: Looping
|
Posts: 5
Name: Nick
|
$text is obviously a string, not an array, so the only way I could think would be parsing the string by lots of splitting
|
|
|
|
04-12-2009, 11:25 AM
|
Re: Looping
|
Posts: 468
|
yeah ive found this how to split it.
PHP Code:
list($type, $id, $player_left, $player_top, $player_name) = split('[,]', $starting_formation);
can anyone tell me the regular expression to seperate each []?
|
|
|
|
04-23-2009, 12:46 PM
|
Re: Looping
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
Here'd be one way:
PHP Code:
<?php $text = "['PLAYER', '12', '-9.5', '140.5', 'Florentino Skears']['PLAYER', '22', '280.5', '160.5', 'Les Lazzo']['PLAYER', '11', '280.5', '120.5', 'Alberto Evan']['PLAYER', '5', '87.2', '215.5', 'Beau Jerome']['PLAYER', '1', '87.2', '140.5', 'Lewis Evan']['PLAYER', '10', '87.2', '65.5', 'Donny Cameron']['PLAYER', '15', '183.9', '230.5', 'Lavern Cable']['PLAYER', '13', '183.9', '170.5', 'Lyle Kozluk']['PLAYER', '17', '183.9', '110.5', 'Bret Ehret']['PLAYER', '26', '183.9', '50.5', 'Shelton White']['PLAYER', '30', '280.6', '215.5', 'Eloy Milne']['PLAYER', '19', '570.5', '140.5', 'Domenic Slater']['PLAYER', '3', '280.4', '190.5', 'Bryce Konstantopoulos']['PLAYER', '29', '280.4', '90.5', 'Benito Basso']['PLAYER', '4', '473.8', '230.5', 'Byran Colin']['PLAYER', '20', '473.8', '170.5', 'Jared Naysmith']['PLAYER', '27', '473.8', '110.5', 'John Pinney']['PLAYER', '16', '473.8', '50.5', 'Deangelo Pugh']['PLAYER', '14', '377.1', '230.5', 'Aurelio Munshower']['PLAYER', '6', '377.1', '170.5', 'Bradley Chauvin']['PLAYER', '21', '377.1', '110.5', 'Darren Wylie']['PLAYER', '18', '377.1', '50.5', 'Will Harris']" ;
$players = explode('][',$text); foreach ($players as $index=>$a_player) { $players[$index] = explode("', '",$a_player); $players[$index][0] = trim($players[$index][0],"'["); $last_index = count($players[$index])-1; $players[$index][$last_index] = trim($players[$index][$last_index],"]'"); } print '<pre>'.print_r($players,true).'</pre>';
?>
I wasn't able to find a single reg ex to do this.
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
04-23-2009, 01:05 PM
|
Re: Looping
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
Quote:
Originally Posted by cammarata123
$text is obviously a string, not an array
|
Strings are arrays.
PHP Code:
//the string you are searching through
$text = "['PLAYER', '12', '-9.5', '140.5', 'Florentino Skears']";
//the strings within the []
$values = array();
$parseStr = false;
$currStr = '';
for($i = 0; $i < strlen($text); $i++)
{
if($parseStr)
{
if($text[$i] == ']')
{
$parseStr = false;
$values[] = $currStr;
$currStr = '';
}
else
{
$currStr .= $text[$i];
}
}
else
{
if($text[$i] == '[')
$parseStr = true;
}
}
|
|
|
|
04-23-2009, 01:09 PM
|
Re: Looping
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
Matt's code is more efficient than mine. Guess I was convicted of being a lazy coder. 
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
04-23-2009, 01:19 PM
|
Re: Looping
|
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
Quote:
Originally Posted by JeremyMiller
Matt's code is more efficient than mine. Guess I was convicted of being a lazy coder. 
|
I'm as guilty as you. I would have done it almost the exact same way as your example 
__________________
I build web things. I work for the startup Please login or register to view this content. Registration is FREE
.
|
|
|
|
|
« Reply to Looping
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|