473,770 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regular expression for parsing html using preg_match_all

Hi all,

I've been trying unsuccessfully to get the text from html page. Html
tag that I'm interested in looks like this:

<a class=link
href="http://www.something.c om/_something.php? type=cart">Shop ping
Cart</a>
<div><em class=newentry> <a href=http://nothing.com>New
Age</a></em></div>
>From the above tag, I want to extract "Shopping Cart". I'm not very
good with RE. I tried this:
$lines = file_get_conten ts("http://theabovetag.com/page.html");
preg_match_all( "/(<a\ class\=link\ href\=(.*)>)(<\/a>)/", $lines,
$matches1);

The above RE gives me "Shopping Cart" plus "New Age" as well. I just
want "Shopping Cart". What am I doing wrong? My RE is somehow ignoring
</atag right after Shopping Cart and instead accepting </aafter New
Age. Please help!

Jul 6 '06 #1
3 3316

cr*********@yah oo.com wrote:
Hi all,

I've been trying unsuccessfully to get the text from html page. Html
tag that I'm interested in looks like this:

<a class=link
href="http://www.something.c om/_something.php? type=cart">Shop ping
Cart</a>
<div><em class=newentry> <a href=http://nothing.com>New
Age</a></em></div>
From the above tag, I want to extract "Shopping Cart". I'm not very
good with RE. I tried this:
$lines = file_get_conten ts("http://theabovetag.com/page.html");
preg_match_all( "/(<a\ class\=link\ href\=(.*)>)(<\/a>)/", $lines,
$matches1);

The above RE gives me "Shopping Cart" plus "New Age" as well. I just
want "Shopping Cart". What am I doing wrong? My RE is somehow ignoring
</atag right after Shopping Cart and instead accepting </aafter New
Age. Please help!
It most likely has to do with the greediness of *. Regular expressions
will match the *longest* possible string. To prevent this, use '?'.
given the string: "<a>text</a>more</a>"
<a>.*</amatches "<a>text</a>more</a>"
<a>.*?</amatches "<a>text</a>"

Jul 6 '06 #2
It most likely has to do with the greediness of *. Regular expressions
will match the *longest* possible string. To prevent this, use '?'.
given the string: "<a>text</a>more</a>"
<a>.*</amatches "<a>text</a>more</a>"
<a>.*?</amatches "<a>text</a>"
Well what i basically want is:
<a class="somethin g" href=http://something.com/abc.php">Shoppi ng
Cart</a>

I want the RE to parse the HTML tag and see if it starts with '<a
class="somethin g" href=', then IGNORE whatever is between 'href=' and
'>', and ending with '</a>'. I couldn't figure out how to "ignore" the
text in between.

Jul 7 '06 #3
cr*********@yah oo.com wrote:
>It most likely has to do with the greediness of *. Regular expressions
will match the *longest* possible string. To prevent this, use '?'.
given the string: "<a>text</a>more</a>"
<a>.*</amatches "<a>text</a>more</a>"
<a>.*?</amatches "<a>text</a>"

Well what i basically want is:
<a class="somethin g" href=http://something.com/abc.php">Shoppi ng
Cart</a>

I want the RE to parse the HTML tag and see if it starts with '<a
class="somethin g" href=', then IGNORE whatever is between 'href=' and
'>', and ending with '</a>'. I couldn't figure out how to "ignore" the
text in between.
Instead of a (greedy) * operator, use a negation class that parses
everything upto an certain character :
/(<a\ class\=link\ href\=([^>]*)>)([^<]*)(<\/a>)/
Jul 10 '06 #4

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

Similar topics

7
2597
by: YoBro | last post by:
Hi I have used some of this code from the PHP manual, but I am bloody hopeless with regular expressions. Was hoping somebody could offer a hand. The output of this will put the name of a form field beside name. I want to get the following but not sure how to modify the code below. 1. Field Name (to appear beside NAME:) 2. Field Type (to appear beside TYPE:)
2
1440
by: hu8 | last post by:
I must find a regular expression to find every declaration af a variable in a php programm..... I tried: /\$()*(\s|\S)\=(\s|\S)/is But it doesn't reall go :-(
6
2293
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not work: it seems that < is not a valid character, so the beginning of the word starts at theletter 'p' instead of '<'. Because I'm not an expert in regular expressions, maybe someone of you guys can help me? I need the correct regex to find the...
25
5167
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
0
286
by: Mike Zhang | last post by:
I got stuck with regular expression problem. What i want to do is to extract hello world if 'message' is found in the same row. I have gotten until //A. but somehow //B doesn't work. <?php $string = '<html> <body> <table> <tr>
0
1146
by: peter stickney | last post by:
Excuse the babbling or lack of sense, it's been a long day. I am making a flat file, static HTML search engine for a site. I downloaded a script from the net and have been working with it for my needs. Everything was working OK with a few test Lorem Ipsum pages. But the moment I try to search real data, fit hits the shan. The script uses a regular expression to search through the files. And
20
14665
by: kosanovic | last post by:
Hello, I'm bad at regular expressions. Would somebody help me: I need to extract all URL to .jpg and .png pictures from a string containing an HTML file (DOM wouldn't work well in what I need). I've tried: preg_match_all("/.jpg$|.png$/", $htmlfile, $Matches); foreach ($Matches as $match) {
2
2362
by: Shahid | last post by:
Hi, I am parsing an .HTML file that contains following example code: <div> <p class="html_preformatted" awml:style="HTML Preformatted" dir="ltr" style="text-align:left"><span style="font-size:12pt;font- family:'Arial'" xml:lang="en-US" lang="en-US">Normal Text Arial 12 Black before bullets.</span></p> <ul> <li class="html_preformatted" dir="ltr" style="text- align:left">&nbsp;<span style="font-size:12pt;font-family:'Arial'"
2
1061
gregerly
by: gregerly | last post by:
Hello All, I'm trying to do some HTML parsing using Regx, but I'm having some issues with my expression. I'm trying to match something like this: <option value="1">Women's 6 <option value="2">Women's 6.5 My expression looks like this: $msg_patt = "/<option value=\"(.*?)\">(+'{1} +({2})?)\n/";
0
9595
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
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
10232
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
10059
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
10008
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
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
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.