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

PREG search for multiple values in one pass?

I am trying to do a search through some data, more specifically HTML,
to extract data from it. So for example I may have:

<b>Title:</b<em>This is a title</em>
<b>Name:</b<em>Fred</em>

I wish to grab the data "This is a title" and "Fred" against their
corresponding headings in an array (e.g. $array[title] = "This is a
title") .... but the key doesn't matter, that need not come from the
regexp but I can do manually.

The question is - how do I do this in one pass? Do I just use use
nested OR's in my regexp? The problem with this though, is once a match
is made, how does PHP then tell me what it corresponds to if it's an
OR? (it just returns the match surely?)

Actually i'm not even too sure how to go about doing this... do I need
backreferencing etc...? I've read the php.net pages but couldn't
entirely make sense of the various preg functions.

Thanks a bunch!

Terence

Aug 19 '06 #1
1 1944
*** te************@gmail.com escribió/wrote (18 Aug 2006 18:46:28 -0700):
I wish to grab the data "This is a title" and "Fred" against their
corresponding headings in an array (e.g. $array[title] = "This is a
title") .... but the key doesn't matter, that need not come from the
regexp but I can do manually.

The question is - how do I do this in one pass?

I'm unsure about your exact needs, but I hope this code helps:
$text='<b>Title:</b<em>This is a title</em>
<b>Name:</b<em>Fred</em>';

if(preg_match_all('@<b>(.*)</b>.*<em>(.*)</em>@Ui', $text, $matches)>0){
print_r($matches);
}
This prints:

Array
(
[0] =Array
(
[0] =<b>Title:</b<em>This is a title</em>
[1] =<b>Name:</b<em>Fred</em>
)

[1] =Array
(
[0] =Title:
[1] =Name:
)

[2] =Array
(
[0] =This is a title
[1] =Fred
)

)
If keys don't matter, must ignore them: '@<b>.*</b>.*<em>(.*)</em>@Ui'

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Aug 19 '06 #2

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

Similar topics

2
by: scott | last post by:
Hi, I'm having some trouble with something that should be relatively easy. I want to update multiple rows in one of my database tables simultaneously. In my table I have these values: ...
17
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
4
by: cwwilly | last post by:
Hello, Thanks for taking a look at this! Problem: I'm trying to pass multiple dynamic values between a slaveform and a masterform. The problem I'm having is on the slaveform I loop through...
5
by: Homer Simpson | last post by:
Hi All, I'm trying to write a method where I pass three arguments and the method returns six values. All the values will be doubles. First, is it possible to get multiple values returned by a...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: dan_williams | last post by:
Is it possible to pass multiple discrete values to a report document so that I can export it to PDF? I've managed to perform the following code to display a Crystal Report Viewer ok, but i want...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
23
blyxx86
by: blyxx86 | last post by:
I am trying to filter multiple entries with only one search box. That way the user can type whatever they want and find all the values that contain what they enter. Private Sub...
3
moishy
by: moishy | last post by:
If I wanted to match for instance, all characters that are not in <TAGS>, I would search for all ">ANYTHING<". But how do I make that "ANYTHING"? What will be the PREG for absolutely ANY...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.