473,372 Members | 1,067 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,372 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 1708
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.