473,766 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_E MPTY 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 1721
tgiles wrote:
however, i've been totally unsuccessful in cleaning out blank entries
in the array (some creep in there, PREG_SPLIT_NO_E MPTY 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_E MPTY 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.c om> wrote in
news:11******** **************@ g14g2000cwa.goo glegroups.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_E MPTY 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_a rray 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($u rl, $urls_array)){
#It's not, so let's add it
array_push($url s_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@fu ng.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.c om> wrote in
news:11******** **************@ g14g2000cwa.goo glegroups.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_E MPTY 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_a rray 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($u rl, $urls_array)){
#It's not, so let's add it
array_push($url s_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
2072
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 28 00 00 880006 CGA 1988 06 21 14 04 G500-051 D 550.62 J.. R1 1993 01 28 00 00 880007 I have to read each line of the table and put it into comma-separated lists like these for later manipulation: ...
11
1683
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 with the data I required preset into them. What I wish to do is split the cookie in which the data is stored and create two array from it. The function used to create the cookie is as follows :function SetCookie(name, value) {...
13
2677
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 transaction, so in general we never have any call to update old rows. Usually, we only deal with analysis on transactions in the current financial year or the previous one, but *occasionally* we'll want to go back further. So I'm thinking as...
20
3717
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 the data to assign each value to its own variable. So right now I am just saving the data to a txt file and when I look in the text file all the data is there. Not sure if this matters but when I open the text file in Word pad (Rich Text) It...
10
2739
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 keep the integrity of the database. We are parsing roughl 81 files and range in size 1 kb to 65 MB files (Average of 400,000 lines in the larger files). I have written this utility with VB.NET 2003 and when I parse all of the files I run out...
28
4296
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 wish to do is split this string in 4 different string values, as
13
1987
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 were i want to separate it and store on a
2
3270
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 lines it worked quite well but srttok isnot working for multiple blank or commas. Can strtok do this kind of splitting if it cant what should i use . Unal
2
230
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 into right array. I've done some coding but I feel this code is very inefficient: void split_array(int *a, int size_of_array)
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.