473,796 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3293
ge********@gmai l.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.goo glegroups.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********@gmai l.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********@gmai l.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********@gmai l.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
3801
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 access the the tags and tags value (<td> <table>, etc) by code. But it just doesn't work in VB.NET but works in VB6. Please someone just show me the right way to do this simple thing.
3
2008
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 text. Is it possible to access in this way "string variable".getElementByID() somehow? Thank you.
7
1758
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 return the entire HTML string when a <p> is part of the text. It somehow throws everything off. I am trying to store exam questions in an Oracle Database as XML. The XML I started with looked like this: <aicpcu id="XMLTEXT"><stem id="STEM">This...
24
6342
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 have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
3
2885
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 it to this page just to show this HTML string. I know that in some cases we can use .InnerHtml property of some controls to inject the HTML string created in C# code, but it didn't work in my case. I tried to use TEXTAREA1.InnerHtml and it shows me...
7
3281
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 ∑ http://xahlee.org/
3
1313
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
16979
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 examples I found on the web. Works in IE and mozilla. http://www.imaputz.com/cssStuff/bigFourVersion.html
0
9679
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
10453
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
10223
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
10172
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
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
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...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.