473,237 Members | 1,244 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 software developers and data experts.

split -command ??

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
2 4869

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
9
by: Will McGugan | last post by:
Hi, I'm curious about the behaviour of the str.split() when applied to empty strings. "".split() returns an empty list, however.. "".split("*") returns a list containing one empty string. ...
2
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces,...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.