By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

split -command ??

P: n/a
Hi,

I was trying to do the following. It's my first php "project", so it's quiet
logic that i have some problems. Perhaps the php community might help. It's
about this :

I have a txt file with the following data :

1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich 2.37; 4. John
Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12;

It's a ranking of a race. Now what i want to reach is, that these names,
come into an array, eventually in an sql database. But the last thing, is
not this big problem, if I succeed in getting it into an array.

So the result might look something like this

Stijn Piot
Kim Van Rooy
....

It would be even cooler, if in front of these names was

1 | Stijn Piot | 58.12
2 | Kim Van Rooy | 1.28
3 | .... | 2.54
....
10| John Terlaeken | 1 ronde/tour/round behind

Now the last thing, I didn't tried, because it seems to complicated for my
first "project". The first however must be possible, and it can't be that
difficult. This is what I had, but it didn't worked. Can somebody help me to
reach the exact result. Thankx in advance

<? php
$textfile = "1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich
2.37; 4. John Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12;"
// this is a part of the textfile

$position=split("\;", $textfile); //splitting in an array with the
semicolon, the result is an array with rows like this 1. stijn piot 58.12
$max = count($position); // counting the size of the array

$i =0;
For ($i=0; $i<$max; $i++) {
$time = split ("^[0-9]+\.[[:space:]]", $position[$i]); // the row 1.
stijn piot 58.12 is splitted according "number space", the result is stijn
piot 58.12
$name = split ("[0-9]+\.[0-9]+", $time[1]);
echo $name[0]; // the previous result stijn piot 58.12 is splitted
according "number.number", the final result should be stijn piot
}
// because it is a for loop, eventually i should have
//stijn piot
//kim van rooy
//.... but the farest i can get is just stijn piot and further nothing.
?>

It's not easy at all, i hope the community can do something for me. I'm sure
that this script can help quiet a lot of people. ;)

Ciao

Stijn
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"nieuws" <st*********@yahoo.com> wrote in message
news:La******************@afrodite.telenet-ops.be...
Hi,

I was trying to do the following. It's my first php "project", so it's quiet logic that i have some problems. Perhaps the php community might help. It's about this :

I have a txt file with the following data :

1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich 2.37; 4. John Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12;

It's a ranking of a race. Now what i want to reach is, that these names,
come into an array, eventually in an sql database. But the last thing, is
not this big problem, if I succeed in getting it into an array.

So the result might look something like this

Stijn Piot
Kim Van Rooy
...

It would be even cooler, if in front of these names was

1 | Stijn Piot | 58.12
2 | Kim Van Rooy | 1.28
3 | .... | 2.54
...
10| John Terlaeken | 1 ronde/tour/round behind

If I had a choice on how the data file would be strcutured, I would go for
the delimited versions
indicated above. I don't like having to parse files with a free structure,
it just makes the code
uneccessarily complex.

I would structure my file as:

1|Stijn Piot |58.12
2|Kim Van Rooy|1.28
3| .... |2.54

The use explode() to split the file contents, instead of split which has the
overhead of having to
process the delimiter as a regular expression.

Thus:

$race_results = file("path_to_text_file");

foreach ( $race_results as $result_line )
{
$participant = $explode('|',$result_line);
$sql = "insert into your_table (postion, name, time)
values('$partcipant[0]','$participant[1]','$participant[2]')";

mysql_query($sql); // IRL we would andle mysql errors here as
well, omitted for clarity
}

Somthing along those lines, see how simple it makes the code?
You might want to add an extra step and trim whitespace off the elements
resulting from the explode.

Thanks,
Mark
---------------------------------------------------------------------------
Windows, Linux and Internet Development Consultant
Email: co*******@scriptsmiths.com
Web: http://www.scriptsmiths.com
---------------------------------------------------------------------------

Now the last thing, I didn't tried, because it seems to complicated for my
first "project". The first however must be possible, and it can't be that
difficult. This is what I had, but it didn't worked. Can somebody help me to reach the exact result. Thankx in advance

<? php
$textfile = "1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich 2.37; 4. John Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12;"
// this is a part of the textfile

$position=split("\;", $textfile); //splitting in an array with the
semicolon, the result is an array with rows like this 1. stijn piot 58.12
$max = count($position); // counting the size of the array

$i =0;
For ($i=0; $i<$max; $i++) {
$time = split ("^[0-9]+\.[[:space:]]", $position[$i]); // the row 1.
stijn piot 58.12 is splitted according "number space", the result is stijn
piot 58.12
$name = split ("[0-9]+\.[0-9]+", $time[1]);
echo $name[0]; // the previous result stijn piot 58.12 is splitted
according "number.number", the final result should be stijn piot
}
// because it is a for loop, eventually i should have
//stijn piot
//kim van rooy
//.... but the farest i can get is just stijn piot and further nothing.
?>

It's not easy at all, i hope the community can do something for me. I'm sure that this script can help quiet a lot of people. ;)

Ciao

Stijn

Jul 16 '05 #2

P: n/a
KAH
"nieuws" <st*********@yahoo.com> wrote in
news:La******************@afrodite.telenet-ops.be:
I have a txt file with the following data :

1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich 2.37;
4. John Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12; $position=split("\;", $textfile); //splitting in an array with the
semicolon, the result is an array with rows like this 1. stijn piot
58.12
$max = count($position); // counting the size of the array
Why are you escaping the semicolon? It does not need escaping, you
should've gotten an error about it (try setting your ERROR_REPORTING to
E_ALL while testing).

$i =0;
For ($i=0; $i<$max; $i++) {
$time = split ("^[0-9]+\.[[:space:]]", $position[$i]); // the row
1.
stijn piot 58.12 is splitted according "number space", the result is
stijn piot 58.12
$name = split ("[0-9]+\.[0-9]+", $time[1]);
echo $name[0]; // the previous result stijn piot 58.12 is splitted
according "number.number", the final result should be stijn piot
}


I don't understand what you're doing here. Why split it several times,
with those strange patterns? Just split it once (looks to me like you're
only interested in the name, and not the time):

$name = split ("[0-9]", $position[$i]);

I recommend you use the PCRE functions, they're faster and have a very
good (IMHO) RegEx engine.

http://www.php.net/manual/en/ref.pcre.php

KAH
Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.