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

a little help with preg_match

Hello... I'm trying to write a regular expression to match the content
of a html tag.

I need to match the content of <h1i.e.

<h1>Hello World</h1>
<h1 class="red_background">Hello World</h1>
<h1><img src="red.gif"/></h1>

etc...

can anyone help me?

Thanks!!
Jun 2 '08 #1
11 2678
..oO(nintesa)
>Hello... I'm trying to write a regular expression to match the content
of a html tag.

I need to match the content of <h1i.e.

<h1>Hello World</h1>
<h1 class="red_background">Hello World</h1>
<h1><img src="red.gif"/></h1>

etc...

can anyone help me?
Try this pattern:

#<h1[^>]*>(.+?)</h1>#

Micha
Jun 2 '08 #2
Michael Fesser ha scritto:
.oO(nintesa)
>Hello... I'm trying to write a regular expression to match the content
of a html tag.

I need to match the content of <h1i.e.

<h1>Hello World</h1>
<h1 class="red_background">Hello World</h1>
<h1><img src="red.gif"/></h1>

etc...

can anyone help me?

Try this pattern:

#<h1[^>]*>(.+?)</h1>#

Micha
It's working!! thanks!
Jun 2 '08 #3
nintesa wrote:
Hello... I'm trying to write a regular expression to match the content
of a html tag.

I need to match the content of <h1i.e.

<h1>Hello World</h1>
<h1 class="red_background">Hello World</h1>
<h1><img src="red.gif"/></h1>

etc...

can anyone help me?

Thanks!!
preg_match('|<h1.*?>(.*?)</h1>|',$b,$matches);
Jun 2 '08 #4
Paul Lautman ha scritto:
nintesa wrote:
>Hello... I'm trying to write a regular expression to match the content
of a html tag.

I need to match the content of <h1i.e.

<h1>Hello World</h1>
<h1 class="red_background">Hello World</h1>
<h1><img src="red.gif"/></h1>

etc...

can anyone help me?

Thanks!!

preg_match('|<h1.*?>(.*?)</h1>|',$b,$matches);

Are all working... but I can't make it work with tag <a>...
Jun 2 '08 #5
..oO(nintesa)
>Are all working... but I can't make it work with tag <a>...
Same thing, if you just want the content of it. Or what's the problem
now? Some more details, please.

Micha
Jun 2 '08 #6
Michael Fesser ha scritto:
.oO(nintesa)
>Are all working... but I can't make it work with tag <a>...

Same thing, if you just want the content of it. Or what's the problem
now? Some more details, please.

Micha
ok...

I have a page content in with I have some links like...

<a href="mypage.php" class="test" alt="good" title="good title">Boing</a>

I'm trying to get out:

mypage.php (and similar)
test (and similar)
good
good title

Boing

etc...

Thanks for your help!
Jun 2 '08 #7
..oO(nintesa)
>I have a page content in with I have some links like...

<a href="mypage.php" class="test" alt="good" title="good title">Boing</a>

I'm trying to get out:

mypage.php (and similar)
test (and similar)
good
good title

Boing

etc...
That's a bit beyond the scope of regular expressions. It would be easier
to use an HTML parser to turn the page into a DOM tree, where you can
use XPath to access any arbitrary node (elements, attributes, values).
Have a look at the DOM extension, especially DOMDocument->loadHTML() to
begin with. See the manual for details and examples.

Micha
Jun 2 '08 #8
nintesa wrote:
Michael Fesser ha scritto:
>.oO(nintesa)
>>Are all working... but I can't make it work with tag <a>...

Same thing, if you just want the content of it. Or what's the problem
now? Some more details, please.

Micha

ok...

I have a page content in with I have some links like...

<a href="mypage.php" class="test" alt="good" title="good
title">Boing</a>
I'm trying to get out:

mypage.php (and similar)
test (and similar)
good
good title

Boing

etc...

Thanks for your help!
Someone did something similar in this thread:
http://groups.google.co.uk/group/com...1a97c30642065d
Jun 2 '08 #9
Someone did something similar in this thread:
http://groups.google.co.uk/group/com...1a97c30642065d

This seems to work:

Jun 2 '08 #10
nintesa ha scritto:
>
>Someone did something similar in this thread:
http://groups.google.co.uk/group/com...1a97c30642065d


This seems to work:

function tag_param_content($content,$tag,$param) {
if ($tag!='img') {
preg_match_all('|<'.$tag.'.*?.$param.'=["\'](.*?)["\'].*?>.*?</'.$tag.'>|',$content,$match);
} else {
preg_match_all('|<'.$tag.'.*?'.$param.'=["\'](.*?)["\'].*?>|',$content,$match);
}
return $match[1];
}
Jun 2 '08 #11
On Apr 27, 12:38*am, nintesa <nint...@nomaaaaail.itwrote:
Hello... I'm trying to write a regular expression to match the content
of a html tag.

I need to match the content of <h1i.e.

<h1>Hello World</h1>
<h1 class="red_background">Hello World</h1>
<h1><img src="red.gif"/></h1>

etc...

can anyone help me?

Thanks!!
$pattern = "/<h1.*?>(.*)<\/h1>/";
test it.
Jun 2 '08 #12

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

Similar topics

2
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100...
5
by: Andrew Richardson | last post by:
Apologies if this has been asked before - I can't find anything on Google or Google Groups. I am running PHP 5.0.4 on Apache 2.0.54 with the PCRE extension installed. For some reason though, the...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
6
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i",...
11
by: callieandmark | last post by:
I have a very simple file upload script which creates a thumbnail of the file (jpg) upon uploading. This works fine with small images, however, if i try to upload a file over about 1mb the...
6
by: Charles | last post by:
Hello, I'm trying to extract the home page URL out of a any URL from the same web site For instance if I'm on http://www.regular-expressions.info/javascriptexample.html I want to extract...
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
8
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ...
5
by: jeddiki | last post by:
Hi, I am wanting to do a simple extraction of the three key header elements from a web page namely these: Is the preg_match() function the best way to find them and put them into variables...
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:
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
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
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: 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...
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
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...
0
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,...

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.