By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,677 Members | 1,877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,677 IT Pros & Developers. It's quick & easy.

Question for the Gurus - preg_replace and HTML

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Thanks Guys, I got it to work.

The help is GREATLY appreciated!

Ion

Jul 17 '05 #7

P: n/a
[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

P: n/a
<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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.