473,394 Members | 2,100 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,394 software developers and data experts.

[preg_match_all] Better algorithm?

Hello

I need to extract some bits of information from a web page, so
I used preg_match_all. I was wondering if there's a simpler algorithm,
or if that's the way it's done:

===========
preg_match_all("|$regex|smi",$buffer,$matches,PREG _PATTERN_ORDER);

$elements=sizeof($matches);
$x=sizeof($matches,TRUE);
$rows = $x/$elements;
$rows--; //Why is the last row empty?

print "elements=$elements, x=$x, $rows<br>";

for ($row=0;$row < $rows;$row++){
$string = "";
for($element=1;$element < $elements;$element++){
$string .= $matches[$element][$row] . ",";
}
//Remove trailing ","
$string = trim($string, ",");
print "$string<hr>";
}
===========

Thank you.
Jun 12 '07 #1
2 1284
On 12.06.2007 20:37 Gilles Ganault wrote:
Hello

I need to extract some bits of information from a web page, so
I used preg_match_all. I was wondering if there's a simpler algorithm,
or if that's the way it's done:

===========
preg_match_all("|$regex|smi",$buffer,$matches,PREG _PATTERN_ORDER);

$elements=sizeof($matches);
$x=sizeof($matches,TRUE);
$rows = $x/$elements;
$rows--; //Why is the last row empty?

print "elements=$elements, x=$x, $rows<br>";

for ($row=0;$row < $rows;$row++){
$string = "";
for($element=1;$element < $elements;$element++){
$string .= $matches[$element][$row] . ",";
}
//Remove trailing ","
$string = trim($string, ",");
print "$string<hr>";
}
===========

Thank you.
Try using foreach to iterate through your arrays.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 13 '07 #2
On Wed, 13 Jun 2007 10:45:48 +0200, gosha bine <st********@gmail.com>
wrote:
>Try using foreach to iterate through your arrays.
I'll try it. Thanks.
Jun 18 '07 #3

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

Similar topics

2
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out...
3
by: Han | last post by:
I know this is possible (because preg can do almost anything!), but can't get a handle on the syntax. I have an HTML string: <font size="3"><a...
2
by: Han | last post by:
The following pattern (which is one subpattern in a string of several) looks for the following $xxx,xxx.xx (with the dollar sign) or xxx,xxx.xx (space in replace of missing dollar sign) ...
0
by: petrovitch | last post by:
While using the following loop to extract images from the google search engine I discovered that preg_match_all works much faster parsing small strings in a loop than extracting all of the urls at...
4
by: Fabian | last post by:
Hi all there, I have already tried asking for help a couple of days ago. I try to rephrase better my problem: I need to grab a webpage that looks like this: <td width=80 align=center...
10
by: greatprovider | last post by:
i'm starting with a string such as "Na**3C**6H**5O**7*2H**20" im attempting to match all **\d+ ...once i can match all the double asterix \d i intend to wrap the \d in "<sub>" tags for display...
1
by: ngmr80 | last post by:
Hi, I'm experiencing a problem when trying to capture substrings with preg_match_all() from strings like "set('Hello','World')" using the following Regular Expression (PERL syntax): ...
6
by: PaulB | last post by:
Hello, as a newbie I'm requesting some help in understanding the regular expression below preg_match_all("|<tr(.*)</tr>|U",$table,$rows); Would anybody please just run through...
2
loriann
by: loriann | last post by:
hi, I have a problem with preg_match function returning empty arrays for my wonderful regexes. However, I can't see what I am doing wrong - maybe one of you could help? I'm loading the source...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.