473,624 Members | 2,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to filter the words in HTML ?

Hi,

I face some problem that I want to filter the all words in HTML.

Example:

Before Filter:
<div id="pp"hello man <br/Thank's for your answer. </div>

After Filter:
<div id="pp"<br/</div>

What I want is reserve all HTML tags but words.
Is there any good packages or classes or suggestion ? Thank you very
much.

Oct 19 '06 #1
2 1536
Ch********@gmai l.com wrote:
Example:

Before Filter:
<div id="pp"hello man <br/Thank's for your answer. </div>

After Filter:
<div id="pp"<br/</div>
I lova good challenges :)
<?php
function get_html($x, $sep=' ') {
$inbrackets = false;
$inquotes = false;
$html = '';
$l = strlen($x);
for ($i = 0; $i < $l; ++$i) {
$y = substr($x, $i, 1);
if (($inbrackets) && ($y == '"')) {
$inquotes = !$inquotes;
}
if ((!$inquotes) && ($y == '<')) {
if ($i 0) {
$html .= $sep;
}
$inbrackets = true;
}
if ($inbrackets) {
$html .= $y;
}
if ((!$inquotes) && ($y == '>')) {
$inbrackets = false;
}
}
return $html;
}

$data = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>ex ample</title></head>
<body>
<div id="pp">
<a name="funky><na me"></a>
<!-- *VALID*^^*HTML* -->
hello man<br/>
Thank's for your answer.
</div>
</body>
</html>
HTML;

$html = get_html($data, "\n");
echo $html;
?>

Or you could try a regular expression (but I'm not sure you could do one
that accepts all valid HTML).

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Oct 19 '06 #2

Ch********@gmai l.com wrote:
I face some problem that I want to filter the all words
in HTML.

Before Filter:
<div id="pp"hello man <br/Thank's for your answer.
</div>

After Filter:
<div id="pp"<br/</div>
Forget regexes. As the saying goes, 'You cannot parse HTML
with regexes'. There's also no reason to write your own
HTML parser -- there already are more than enough of those.

XSLT was meant exactly for this type of processing, and it
doesn't really care what you're processing, as long as it's
a DOMDocument.

Using PHP5's DOM and XSL modules:

<?php
$xml_str =
'<div id="pp"><phell o man <br/Thank\'s for your ' .
'answer. </div>' ;
$xsl_str =
'<xsl:styleshee t ' .
' xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" ' .
' version="1.0">' .
' <xsl:template match="node()|@ *">' .
' <xsl:copy>' .
' <xsl:apply-templates select="node()| @*"/>' .
' </xsl:copy>' .
' </xsl:template>' .
' <xsl:template match="html">' .
' <xsl:apply-templates/>' .
' </xsl:template>' .
' <xsl:template match="body">' .
' <result>' .
' <xsl:apply-templates/>' .
' </result>' .
' </xsl:template>' .
' <xsl:template match="text()"/>' .
' </xsl:stylesheet> ' ;

$xml = DOMDocument :: loadHTML ( $xml_str ) ;
$xsl = DOMDocument :: loadXML ( $xsl_str ) ;
$xform = new XSLTProcessor ( ) ;
$xform -importStyleshee t ( $xsl ) ;
$result = $xform -transformToDoc ( $xml ) ;
header ( 'Content-type: text/xml' ) ;
print ( $result -saveXML ( ) ) ;
?>

If you're using real XHTML (as opposed to mumbo jumbo tag
soup pretending to be XHTML), it's even better, because you
don't have to pretend you're processing XML. XHTML *is*
XML.

--
Pavel Lepin

Oct 20 '06 #3

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

Similar topics

13
7762
by: bgbauer70 | last post by:
My appologies if this ends up being a duplicate post. For some reason the first post never showed up. I've tried about 300 iterrations of this same ability, and none of them seem to work in Firefox. Take the following code for example. It WILL stop me from entering zero into the first text box, but it wont stop me from entering extended ascii characters (which is the final goal).
4
1995
by: David Pratt | last post by:
Hi. Is anyone aware of any python based unacceptable language filter code to scan and detect bad language in text from uploads etc. Many thanks. David
3
6603
by: Richard | last post by:
Hi, I have a form based on a table. When I filter the form I want to run a report based on the same table with the same filter as the form. No problem until I want to filter a combo box where the text value is on a different table. The me.filter is then a text instead of the id-number from the lookup table. This causes the report to prompt for the parameter. How do I get by this problem? Do I need to create a temporary table? I rather...
4
1664
by: Nathan Bloomfield | last post by:
I am using the code below to filter a report: Private Sub Set_Filter_Click() If Me! And Me! <> "" Then If Application.CurrentProject.AllReports("rptDailyOutput").IsLoaded Then DoCmd.Close acReport, "rptDailyOutput" DoCmd.OpenReport "rptDailyOutput", acViewPreview, , " Between #" & Format(Me!, "mm/dd/yy") & "# and #" & Format(Me!, "mm/dd/yy") & "#"
4
1804
by: Thomas | last post by:
I want to be able to implement a filter that manipulates the output from the server... Maybe replacing some words, or highlighting a search string, or some other fancy feature... I want to make this code reusable to all my applications, and by making this as a HttpModule I can implement this feature on existing .Net applications by editing the we.config and nothing else... My problem is that that I can't get this to work.
2
1901
by: amir | last post by:
How can I filter my database in Multiple way, I mean that I wana to filter st. like this: sm* *th* *er Please Help me on this matter Amir
3
1988
by: Mr 200 | last post by:
Hi all ive just recently started learning java and was happy to find a forum that may be able to give me a little assistance. I have been set an excersize to create a filter processor that relays information between several filters to give a result. I have created one of the filters already, however i need the filter processor working in order to test wether the filter i have done works correctly. However im not completely sure where to...
0
1963
by: mark r | last post by:
Hi there... im on with my shoutbox again only now i want to add a bad words filter. ive got the following code off a website and would appreciate someone showing me how to integrate it into my code... the bad words filter code is: <% Dim sMyString sMyString = ReplaceBadWords("this is a rubbish crap bad word filter") response.write sMyString Function ReplaceBadWords(InputComments)
6
1887
by: cmartin1986 | last post by:
What I'm trying to do is add a button to my "front" page which will open a form and filter excluding the words "installed" and "na" in the field declared by my combo box. I've pretty much got it figured out except I can get it to filter out more than one of the two words. Here is what I've got: Private Sub Command82_Click() On Error GoTo Err_Status_Click Dim stdocname As String Dim stlinkcriteria As String stdocname =...
0
8182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8352
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
8494
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
7178
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...
0
4085
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
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.