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

simple question for grabbing html tags out of string

Good morning. I am racking my brains over what seems like should be a
simple question. I have a string that contains text and html.
Basically, I would like to grab the HTML tags from the string and wrap
some comment tags around it. Does anyone know how I would do this? I
was thinking about using the str_replace function, but this won't work
for certain html tags, such as <A href=#"> since the "#" won't be known
beforehand. Maybe a simple regular expression? Thank you so much for
your help!

Gene

Jul 17 '05 #1
7 3265
ge********@gmail.com wrote:
Basically, I would like to grab the HTML tags from the string and wrap
some comment tags around it. Does anyone know how I would do this? I
was thinking about using the str_replace function, but this won't work
for certain html tags, such as <A href=#"> since the "#" won't be known
beforehand. Maybe a simple regular expression? Thank you so much for
your help!


You asked such a question before:
news:11**********************@g14g2000cwa.googlegr oups.com

But never replied on followups...

Jul 17 '05 #2
Hey Daniel,

Thanks for the advice. The str_replace code is giving me a parse error.
It is saying "Parse error: parse error, unexpected ';" Do you know
where the error is taking place? Maybe a quote or something? Thank you.

Gene

Jul 17 '05 #3
NC
ge********@gmail.com wrote:

I am racking my brains over what seems like should be a
simple question. I have a string that contains text and html.
Basically, I would like to grab the HTML tags from the string
and wrap some comment tags around it.


Why? Wouldn't it be easiser to just strip the tags altogether?

But if you insist on doing it your way, here you go:

$string = "<p>This is HTML.</p>";
$string = str_replace('<', '<!-- <', $string);
$string = str_replace('>', '> -->', $string);

Cheers,
NC

Jul 17 '05 #4
Hey NC,

Thanks for the advice. One question. Do you know of a way for it to
distinguish html tags from < and > signs? For example, I would like
this script to work on <p> but leave > and < alone. Thanks.

Gene

Jul 17 '05 #5
ge********@gmail.com wrote:
Basically, I would like to grab the HTML tags from the string and wrap
some comment tags around it.


Would removing the tags completely work okay?

strip_tags($s);

Cheers,
Nicholas Sherlock
Jul 17 '05 #6
NC
ge********@gmail.com wrote:

I would like this script to work on <p>
but leave > and < alone.


< and > have no place in HTML. The fact that browsers tend
to display them correctly when they are not part of an HTML
tag is sheer goodwill on the part of browser developers and
should not be counted on. &lt; and &gt; should be used
instead.

Cheers,
NC

Jul 17 '05 #7
Somebody wrote:

[referring to str_replace, I suppose]
Do you know of a way for it to distinguish html tags from < and > signs?


Use a proper parser.

Once more and this must be a FAQ.

--
Jock
Jul 17 '05 #8

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

Similar topics

3
by: Joe | last post by:
Hi, I have a simple thing I need to do but just doesn't work in VB.NET. I have a string with HTML code and I want to load it into a HTMLDocument object or something similiar to it so I can...
3
by: Alex | last post by:
Hello. First, with AJAX I will get a remote web page into a string. Thus, a string will contain HTML tags and such. I will need to extract text from one <span> for which I know the ID the inner...
7
by: MaryA | last post by:
Let me preface this with the fact that I am a newbie to HTML, XML and Javascript. Having said that, let me explain my dilemma: I am having a difficult time getting innerHTML to consistently...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
3
by: Just D. | last post by:
All, What's the simplest way to show my own HTML string on the ASPX page assuming that this page is just created using the wizard and it has nothing on it? We're free to use any control adding...
7
by: Xah Lee | last post by:
Summary: when encountering ex as a unit in css, FireFox (and iCab) did not take into account the font-family. Detail: http://xahlee.org/js/ff_pre_ex.html Xah xah@xahlee.org ∑...
3
by: harvey | last post by:
How do I get a text string that may contain HTML tags to echo out to a web browser verbatim? IE I want the tags to be printed out - not interpreted. Thanks
1
by: since | last post by:
I figured I would post my solution to the following. Resizable column tables. Search and replace values in a table. (IE only) Scrollable tables. Sortable tables. It is based on a lot...
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
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.