Welcome Guest [Log In] [Register]
Add Reply
How would i strip bad parts of code out of good tags?
Topic Started: Aug 23 2009, 12:59 AM (428 Views)
Knight13

0
Edited by Knight13, Mar 19 2010, 06:55 AM.
Offline Profile Quote Post Goto Top
 
Darksorrow131

I have no clue what the standard way to do this kind of thing is... here are some ways I thought of, but none of them feel very good
- convert ALL tags to HTML entities (&lt, &gt, and stuff), then regexp search for "&lt/?(b|i|a|hr|img|blahblah)&gt"
- regexp search all "<blahblah>", compare the tag with your list, and get rid of them if it's not in the list
- get an HTML parser library somewhere
but all of these methods feel pretty weird.
(let loop () (loop))
((lambda (x) (x x)) (lambda (x) (x x)))
(let ((k #f)) (call/cc (lambda (cc) (set! k cc)) (k))
((call/cc call/cc) (call/cc call/cc))

-- Infinite loops are awesome! --
Tell me if you have other awesome infinite loops!
Offline Profile Quote Post Goto Top
 
cyrus709

I know that this thread is a bit old but as it has not been answered i will answer it ;) Recently i have done something similar accept i used BB codes rather then actual html tags. I wrote the script myself and am proud to say it works well for me.

1st) I strip the text of all html tags with strip_tags
2nd) I put on a word wrap (becouse of how my design is, there can only be so many words per line)
3rd) I user preg_replace to search the document for BB codes and replace the bb codes with html.
4th) I update my users profile text

Here is my code that i used with comments to help you out
Code:
 

<?php
//I make sure that the person editing the profile is signed in,

session_start();
if($_SESSION['logged'] != 1){ header("location:index.html"); }

// I use the session variable to know which profile to edit, this way the user can only edit their own profile
$convert= $_SESSION['username'];

//connects to mysql
$con = mysql_connect("localhost", "root", "");
if(!$con)
{
die();
}

//selects DB
mysql_select_db("text", $con);

//Gets text which i get from the page where they actualy enter the text
$text = $_POST['profiletxt'];

/*strips html tags */
$text = strip_tags($text);

//text can not be longer then 45 words
$text = wordwrap($text, 45, "<br>");


/*specifies and replaces BB codes with appropriate html
currently all bb codes are here, some need to be taken out */

$text = preg_replace ('/\[font color="([a-zA-Z0-9\#]+)"\]/' , '<font color="\\1">' , $text );
$text = preg_replace ('/\[font size="([0-9]+)"\]/' , '<font size="\\1">' , $text );
$replace = array("[b]", "[/b]", "[/font]", "[graph]", "[/graph]", "[i]", "[/i]", "[u]", "[/u]", "[img]", "[/img]");
$BBC = array("<b>", "</b>", "</font>", "<img src=http://localhost/Global%20C/profilegraph.php?username=", ">", "<i>", "</i>", "<u>", "</u>", "<img src=\"", "\">");

$text = str_replace($replace, $BBC, $text);
print $text;

//Update the profile
mysql_query("UPDATE users SET Profile = '$text' WHERE username = '$convert'");

//send them back to their own profile so they can view the text
header("location: profile.php?username=" . $convert);
?>


Basicly you need to look into
str_replace
preg_replace (for font color etc.)
strip_tags
Therapy is expensive, poppin' bubble wrap is cheap! You choose.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Web-Based Programming Support · Next Topic »
Add Reply

Banner and Logo by TheKeith