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

Question for the Gurus - preg_replace and HTML

Hey all!

I am having a bit of trouble getting this to work.

You all are probably familiar with the code that strips HTML tags:
$PageText = preg_replace("/<.+?>/","",$WebPage);

What I need to do is the exact oposite, I want to strip the page text
and keep the HTML tags.

Lets say I have: "Hello <b>World</b>"
I want to get: "<b></b>"

Thanks in advance
Ion

Jul 17 '05 #1
9 5036
On 9 Feb 2005 09:25:52 -0800, <ih****@hotmail.com> wrote:
Hey all!

I am having a bit of trouble getting this to work.

You all are probably familiar with the code that strips HTML tags:
$PageText = preg_replace("/<.+?>/","",$WebPage);

What I need to do is the exact oposite, I want to strip the page text
and keep the HTML tags.

Lets say I have: "Hello <b>World</b>"
I want to get: "<b></b>"

Thanks in advance
Ion

preg_match_all("/<[^>]+>/",$WebPage,$match);

You will then have all the tags in a nice array.
Just implode the array if you want it in a string.

--
Stian
Jul 17 '05 #2
ih****@hotmail.com writes:
You all are probably familiar with the code that strips HTML tags:
$PageText = preg_replace("/<.+?>/","",$WebPage);

What I need to do is the exact oposite, I want to strip the page text
and keep the HTML tags.


Ok sinple enough. You'll have to test this yourself though.

replace >.*?< with ><

In other words, since content will be found between tags of whatever
sort, replace that with only the delimiters and you should have it.
Not optomized and will do replacements where not really needed. Have
fun.

Get it right and go mad trying most optimal (er, elegant?) solution
using negative lookahead/behind!

HTH

--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Jul 17 '05 #3
Just tried it,

preg_match_all("/<[^>]+>/", $WebPage,$match) seems to be creating an
array "$match" with one element that simply says "Array" in pos [0]. I
know I must be doing something wrong (usually obvious).

Any ideas?

Jul 17 '05 #4
The Following:

preg_match_all("/<[^>]+>/", "Hello<b>World</b>Bye", $match);
echo "00: " . $match[0] . "<br>";
echo "01: " . $match[1] . "<br>";
echo "02: " . $match[2] . "<br>";

Is Giving Me:

00: Array
01:
02:

Not sure what is wrong.
Thanks in advance for help!
Ion

Jul 17 '05 #5
On 9 Feb 2005 12:06:39 -0800, <ih****@hotmail.com> wrote:
Just tried it,

preg_match_all("/<[^>]+>/", $WebPage,$match) seems to be creating an
array "$match" with one element that simply says "Array" in pos [0]. I
know I must be doing something wrong (usually obvious).

Any ideas?


When you use preg_match, and want to store the matched results you get
the complete match in $match[0], and stuff matched in capturing
parantheses is placed in $match[1] and up. As we are not using any
capturing parantheses you only get one array. The info you want is
inside this again. To see this more clearly try print_r($match);,
or you could implode('',$match[0]);

The solution to Jerry posted earlier is easier if you
want the tags directly in a string.

--
Stian
Jul 17 '05 #6
Thanks Guys, I got it to work.

The help is GREATLY appreciated!

Ion

Jul 17 '05 #7
[Anonymous -- J.D.] wrote:
You all are probably familiar with the code that strips HTML tags:
Parsing arbitrary HTML should be done with a parser rather
than a single, ad hoc regular expression. Predetermined
HTML, on the other hand, can be parsed by a regular
expression because you know beforehand its exact form.
$PageText = preg_replace("/<.+?>/","",$WebPage);


Performs pretty much the same function as strip_tags, which
for the most part is too simplistic to be of any practical
worth.

--
Jock
Jul 17 '05 #8
<ih****@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hey all!

I am having a bit of trouble getting this to work.

You all are probably familiar with the code that strips HTML tags:
$PageText = preg_replace("/<.+?>/","",$WebPage);

What I need to do is the exact oposite, I want to strip the page text
and keep the HTML tags.

Lets say I have: "Hello <b>World</b>"
I want to get: "<b></b>"

Thanks in advance
Ion


If your code is meant to handle any HTML pages, then be mindful of
Javascript. Code snippets have to be removed first, for otherwise code that
happens to lie between a < and > operator would be incorrectly interpreted
as being a tag.

Something like this should do it:

preg_replace("/<script.*?>.*?<\/script\s*>/", "", $webpage);

Jul 17 '05 #9
On Wed, 9 Feb 2005 21:06:29 -0500, Chung Leong <ch***********@hotmail.com>
wrote:
<ih****@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hey all!

I am having a bit of trouble getting this to work.

You all are probably familiar with the code that strips HTML tags:
$PageText = preg_replace("/<.+?>/","",$WebPage);

What I need to do is the exact oposite, I want to strip the page text
and keep the HTML tags.

Lets say I have: "Hello <b>World</b>"
I want to get: "<b></b>"

Thanks in advance
Ion


If your code is meant to handle any HTML pages, then be mindful of
Javascript. Code snippets have to be removed first, for otherwise code
that
happens to lie between a < and > operator would be incorrectly
interpreted
as being a tag.

Something like this should do it:

preg_replace("/<script.*?>.*?<\/script\s*>/", "", $webpage);


If javascript is a problem, and you don't want to remove them,
there is a solution:
preg_match_all("/<(?:(?=script>)|(?!(?!.*?<script>).*?<\/script>))[^>]+>/s",$webpage,$match);
It's ugly, I know, but i think it might work.
It says something like:
<(?:(?=script>) #Match tag start followed by script
| #Or
(?!(?!.*?<script>).*?<\/script>))[^>]+> #Tags wich is not within script

I ran this on an example:
<html>
<body>
<script>
var i = 10;
if(i<10) {
} else if(i>10) {
} else {
}
</script>
This <b>is a</b> Test
</body>
</html>

And the output is:
[0] => Array
(
[0] => <html>
[1] => <body>
[2] => <script>
[3] => </script>
[4] => <b>
[5] => </b>
[6] => </body>
[7] => </html>
)
Notice that you don't get a tag called "<10) {} else if(i>".

--
Stian
Jul 17 '05 #10

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

Similar topics

4
by: Sam | last post by:
Hi Can anyone help me with this problem please? I have a long string that I would like to split into paragraphed sentences. I would like to replace every full-stop or period ('.') with the...
5
by: paul brown | last post by:
howdy, I have some text: This is {search}Pam Grier{/search}. What I want is this: This is <a href="somepage.php?keyword=Pam Grier">Pam Grier</a>.
4
by: Sidharta | last post by:
Hi all, how come this doesn't work????? # convert to unix new lines $text = preg_replace("/\r\n/", "\n", $text); # remove extra new lines $text = preg_replace("/\n+/", "\n", $text); is...
5
by: michel | last post by:
Hi group, just wondering: I have a database, containing the following fields: name address zip email
1
by: yawnmoth | last post by:
say i have the following script: <? $test = "aaaaa"; print '"' . preg_replace('/.*/','x',$test) . '"<br>'; $test = "\n\n\n\n\n"; print '"' . preg_replace('/.*/','x',$test) . '"'; ?> the...
7
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore...
2
by: Afkamm | last post by:
Hi, :) The preg_replace function... preg_replace(pattern, replacement, subject ) How on earth do you get the limit value to work with arrays? In my code both the pattern and replacement...
1
by: Dave | last post by:
Hi, This is an interesting problem I'm faced with. I have been trying all sorts of functions to fix it and my last resort is to ask you guys and girls. I have an array:...
10
by: deko | last post by:
I need to capture referrers to a website and populate a page with links to the referring pages. How do I use preg_replace to search for and replace all ampersands ('&') with the corresponding...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?
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
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
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,...
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...

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.