473,287 Members | 1,880 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,287 software developers and data experts.

splitting lines in arrays?

Hi, all.

Been staring at this for a couple of hours now and I find myself
completely bewildered. I suppose it doesn't help that I'm a php newbie.
Nevertheless, I throw myself at your mercy.

I have an array which I am attempting to split off into a new array.
The first array is just a whole bunch of links like so:

http://www.example.com/query?track=h...m/whatever.php
http://www.example.com/query?track=h.../whatever1.php

I managed to (sorta) successfully extract things out using preg_split:

foreach ($http_array as $i) {
$split = preg_split("/=/", $i, 2);
print "$split[1]<br>";
}

and the output looks like

http://www.somewhereelse.com/whatever.php
http://www.somewhereelse.com/whatever1.php
.... and so on...

The main goal is to parse an array much like the first bit at the top,
lop off anything after the first "=" in the line and then place it all
into another blank array so that I might do something else clever
elsewhere with it.

however, i've been totally unsuccessful in cleaning out blank entries
in the array (some creep in there, PREG_SPLIT_NO_EMPTY does not seem to
catch them) and cramming all of that into another new array. Been back
and forth between google and the fine php documents with little to show
for it.

eh. I give up. thoughts?

tom

Jul 17 '05 #1
4 1704
tgiles wrote:
however, i've been totally unsuccessful in cleaning out blank entries
in the array (some creep in there, PREG_SPLIT_NO_EMPTY does not seem
to catch them) and cramming all of that into another new array. Been
back and forth between google and the fine php documents with little
to show for it.


PREG_SPLIT_NO_EMPTY doesn't work for you, because your error_reporting level
is set too low.

If you would prepend the following to your code, you will see a warning when
the split didn't succeed:

error_reporting(E_ALL);

As you will see, $split[1] isn't always set and doing an isset($split[1]) or
a count($split[1]) test before populating the target array helps to skip
empty elements.

As an alternative, you can also use preg_match to test for matches:

foreach ($http_array as $i) {
if (preg_match("/=(.+)/", $i, $split)) {
print "$split[1]<br>";
}
}
JW

Jul 17 '05 #2
"tgiles" <tg****@gmail.com> wrote in
news:11**********************@g14g2000cwa.googlegr oups.com:
Hi, all.

Been staring at this for a couple of hours now and I find myself
completely bewildered. I suppose it doesn't help that I'm a php
newbie. Nevertheless, I throw myself at your mercy.

I have an array which I am attempting to split off into a new array.
The first array is just a whole bunch of links like so:

http://www.example.com/query?track=h...se.com/whateve
r.php
http://www.example.com/query?track=h...se.com/whateve
r1.php

I managed to (sorta) successfully extract things out using preg_split:

foreach ($http_array as $i) {
$split = preg_split("/=/", $i, 2);
print "$split[1]<br>";
}

and the output looks like

http://www.somewhereelse.com/whatever.php
http://www.somewhereelse.com/whatever1.php
... and so on...

The main goal is to parse an array much like the first bit at the top,
lop off anything after the first "=" in the line and then place it all
into another blank array so that I might do something else clever
elsewhere with it.

however, i've been totally unsuccessful in cleaning out blank entries
in the array (some creep in there, PREG_SPLIT_NO_EMPTY does not seem
to catch them) and cramming all of that into another new array. Been
back and forth between google and the fine php documents with little
to show for it.

eh. I give up. thoughts?


<?php

//$http_array is already getting filled up somewhere else

$urls_array = array();

foreach($http_array as $i){
#Break the line into two parts, saving the stuff after "track="
list($junk, $url) = split('track=', trim(rtrim($i)));
#Make sure $url contains a value
if(!empty($url)){
#See if $url is already in the $urls_array array
if(!in_array($url, $urls_array)){
#It's not, so let's add it
array_push($urls_array, $url);
}
}
}

?>

Now you have a new array, $urls_array, which contains the URLs you want
to work some magic on.

hth
--

Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
--------------------------|---------------------------------
<http://www.phplabs.com/> | PHP scripts, webmaster resources
Jul 17 '05 #3
Senator Jay Billington Bulworth wrote:
#Break the line into two parts, saving the stuff after "track="
list($junk, $url) = split('track=', trim(rtrim($i)));


Not a very good example, because:

1. Using split instead of explode creates a lot of overhead
2. You will get a warning when $i cannot be split into 2 elements and the
error reporting level is set to E_ALL
JW

Jul 17 '05 #4

Senator Jay Billington Bulworth wrote:
"tgiles" <tg****@gmail.com> wrote in
news:11**********************@g14g2000cwa.googlegr oups.com:
Hi, all.

Been staring at this for a couple of hours now and I find myself
completely bewildered. I suppose it doesn't help that I'm a php
newbie. Nevertheless, I throw myself at your mercy.

I have an array which I am attempting to split off into a new array. The first array is just a whole bunch of links like so:

http://www.example.com/query?track=h...se.com/whateve r.php
http://www.example.com/query?track=h...se.com/whateve r1.php

I managed to (sorta) successfully extract things out using preg_split:
foreach ($http_array as $i) {
$split = preg_split("/=/", $i, 2);
print "$split[1]<br>";
}

and the output looks like

http://www.somewhereelse.com/whatever.php
http://www.somewhereelse.com/whatever1.php
... and so on...

The main goal is to parse an array much like the first bit at the top, lop off anything after the first "=" in the line and then place it all into another blank array so that I might do something else clever
elsewhere with it.

however, i've been totally unsuccessful in cleaning out blank entries in the array (some creep in there, PREG_SPLIT_NO_EMPTY does not seem to catch them) and cramming all of that into another new array. Been back and forth between google and the fine php documents with little to show for it.

eh. I give up. thoughts?
<?php

//$http_array is already getting filled up somewhere else

$urls_array = array();

foreach($http_array as $i){
#Break the line into two parts, saving the stuff after "track="
list($junk, $url) = split('track=', trim(rtrim($i)));
#Make sure $url contains a value
if(!empty($url)){
#See if $url is already in the $urls_array array
if(!in_array($url, $urls_array)){
#It's not, so let's add it
array_push($urls_array, $url);
}
}
}

?>

Now you have a new array, $urls_array, which contains the URLs you

want to work some magic on.

hth


Cheers, Senator. Worked the first time. The comments were helpful as
well in helping me figure out exactly what was going on.

I appreciate the help

tgiles

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: robsom | last post by:
Hi, I have a problem with a small python program I'm trying to write and I hope somebody may help me. I'm working on tables of this kind: CGA 1988 06 21 13 48 G500-050 D 509.62 J.. R1 1993 01...
11
by: Steve Darby | last post by:
Can anyone help with this problem. I am attempting to dynamically draw a graph using data from a cookie. I have written the script to actually draw the graph, for which I hav created two arrays...
13
by: James Conrad St.John Foreman | last post by:
One of the larger tables in our database is now 6.8 million rows (1 per financial transaction since 2000). Every time an amendment is made to a booking, new rows are added to the table for each...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
10
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to...
28
by: Materialised | last post by:
Hi all, Just wondering if someone could help me with this little problem I'm having. I have a string value (it actually represents a barcode) which looks like this: 5021378002392 What I...
13
by: Pedro Pinto | last post by:
Hi there. I'm trying to do the following. I have a string, and i want to separate it into other halves. This is how it should be: char string = "test//test2//test3"; were // is the part...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
2
by: pereges | last post by:
I've an array : {100,20, -45 -345, -2 120, 64, 99, 20, 15, 0, 1, 25} I want to split it into two different arrays such that every number <= 50 goes into left array and every number 50 goes...
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
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.