Jump to content

Preg_replace to remove all except HTML and Alphanumeric


paladin_sh

Recommended Posts

Hey There,

I've having some trouble with Preg_Replace. I need it to remove everything except the Alphanumeric Characters of a string and the HTML.

I want anything between the HTML brackets to be allowed, despite the Removal of everything non-alphanumeric. This way I can sort it out with the tags() function to only allow the HTML I want.

I used this to remove Alphanumeric:

[code]$string = preg_replace("/[^A-Za-z1-9]/","",$string);[/code]

But I need that statment to ignore anything between HTML tags too.

Thanks for any help that is offered.

- Paladin
Link to comment
Share on other sites

Thanks,

I read through the topic, and while I can remove HTML code, and mess around with the stuff inside it I can't get my script to remove everything 'except' the HTML. Let alone get it to remove everything except the HTML and the Alphanumerics. When I give it a try, removing everything but the HTML, it just removes all the spaces and leaves everything else. hehe.

I admit, Regex isn't my strong suit, and I am still learning quite a bit about it especially how it is used in PHP. I learn mostly by example.

I do appreciate the help so far though.

- Paladin
Link to comment
Share on other sites

[code]
<pre>
<?php

$html = <<<HTML
<html>
<head><title>T%i#t*l@e!</title></head>
<body>
abcde 12345
<font color="red">|+_()*!@#$%</font>
</body>
</html>
HTML;

$html = preg_replace_callback(
'/(?<=>)([^<]+)/',
create_function(
'$matches',
'return preg_replace("/\W/", "", $matches[0]);'
),
$html
);

echo htmlspecialchars($html);

?>
</pre>
[/code]

If you want to preserve the whitespace, change[tt] /\W/ [/tt]to[tt] /(?![\s])\W/[/tt]. Also, note that the[tt] w/W [/tt]shorthand includes/excludes an underscore as well, so you may want to change this.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.