Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing HTML special Chars from a string! pls Help 1

Status
Not open for further replies.

Forri

Programmer
Oct 29, 2003
479
MT
Hi all

I need to remove certain HTML tags within a string! how can this be done?

the HTML tags i want to be removed are: <font....> </font>
and anything that has style="font-face:...."

Any ideas pls?

Thanks
Nick
 
Hi!

php has a very cool function, called strip_tags().

You can define which tags you want to keep, as an optional, second parameter!

eg:
Code:
// your string (just for testing)
$string = "Hi! <font size=\"\">boo!</font><br />Look out!!<p>yo!</p>";

// allowed tags
$allowed = "<b><p><br>";

// lets show you what you had
echo $string . "<hr />";

// remove unwated tags
$string = strip_tags($string, $allowed);

// lets see what you have now :)
echo $string;

You can ofcourse make it all in one line:
Code:
echo strip_tags($string, '<b><p><br>');

Good luck :)

Olav Alexander Mjelde
Admin & Webmaster
 
oh.. btw.

this is not referred to as "special chars"

html special chars, is like this:
" " (space) = &nbsp;
< = &lt;
> = &gt;
& = &amp;

the special chars are used, so there will be less risk of compatability issues..

eg. I'm from Norway, if I write the letter å, you will not see it correctly. Also if I use it in url's, it will not work for you, since your characterset is different.

that's why I would rather write it as &aring; in the html code, aka. special characters.

ref.:
Olav Alexander Mjelde
Admin & Webmaster
 
Thanks Olav...i'll give it a try and let you know! cheers!

Nick
 
Ok, good luck.

I'm off now, for some hrs. I have to deliver a car to get EU controlled. (emission, brakes, etc.)

Good luck!

ps. read up on the ref. I gave you, if you wish to make xhtml pages.

Olav Alexander Mjelde
Admin & Webmaster
 
olav...

it worked great! yes you're pretty right when you said...the <> are not special tags..sorry my mistake and thanks for correcting me!

One last thing...i need to remove the style from within the <p> tag or any other tag that contains it! is there are way to do so?

Thanks
Nick
 
hmm.. I think you need to first search for the style="

if you find it, search for first occurance of "

use those integers to substr and fix on the string..

this is quite "messy", maybe there are some samples of this if you search google.

Olav Alexander Mjelde
Admin & Webmaster
 
You can use a regular expression to remove style attributes.
Another way would be to use the DOM functions - if you have them available in your PHP installation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top