Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 04-12-2009, 11:00 AM Looping
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
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..
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
 
Register now for full access!
Old 04-12-2009, 11:19 AM Re: Looping
Novice Talker

Posts: 5
Name: Nick
Trades: 0
$text is obviously a string, not an array, so the only way I could think would be parsing the string by lots of splitting
cammarata123 is offline
Reply With Quote
View Public Profile
 
Old 04-12-2009, 11:25 AM Re: Looping
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
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 []?
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 04-23-2009, 12:46 PM Re: Looping
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 04-23-2009, 01:05 PM Re: Looping
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by cammarata123 View Post
$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;
     }

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-23-2009, 01:09 PM Re: Looping
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 04-23-2009, 01:19 PM Re: Looping
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by JeremyMiller View Post
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
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to Looping
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.16966 seconds with 12 queries