Unstoppable Domains

[Resolved] Code Filter Help!!! In PHP

Spaceship Spaceship
Watch

purity

Established Member
Impact
1
Code Filter Help!!! in PHP

Hello Im trying to make a comments system and well i want it to be able to filter coding like html and stuff turning the < > into the symble code and id also like it to turn spaces into <br />

I've used the code before but i cant remember it :(

also i want it to work on the submit so like under
$comment=$_POST['comment'];
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
Okay, you can replace variables really easily, say you wanted to replace line breaks with <br /> you should be able to do

PHP:
$comment = str_replace("\r","<br />",$comment);

Play around with that, i think you should either be replacing \r or \n... or both :P
 
0
•••
:D thanks heaps :D the code works 100% perfect :D

can i also use that replace for smiles and code tags?
 
0
•••
Yup it can replace whatever you want, you can also use arrays to set a load of things to be replaced, for example

PHP:
$badstuff = array(':)',"\r",':(');
$goodstuff = array('<img src="smiley.gif">','<br />','<img src="sad.gif">');
$comment = str_replace($badstuff,$goodstuff,$comment);
 
0
•••
:D great stuff thanks heaps for all yourself
 
0
•••
Purity, if you wanted to use BBCodes too...

I wouldn't advise you to use str_replace, preg_replace would be better.
This would also allow you to use likes such as
So, you can add URL's, and the sitename etc in...better for quotes too, such as Someone said.

Here's the code, untested.

PHP:
<?php

Class BBCode
    {

	 var $BBCode = array(
                '/(\[url=)(.*)(\])(.*)(\[\/url\])/i',
                '/(\[url\])(.*)(\[\/url\])/i',
                '/(\[email=)(.*)(\])(.*)(\[\/email])/i',
                '/(\[email])(.*)(\[\/email])/i',
                '/(\[quote=)(.*)(\])(.*)(\[\/quote])/i',
                '/(\[quote\])(.*)(\[\/quote\])/i',
                '/(\[b\])(.*)(\[\/b\])/i',
                '/(\[i\])(.*)(\[\/i\])/i',
                '/(\[u\])(.*)(\[\/u\])/i',
                '/(\[img\])(.*)(\[\/img\])/i',
                '/(\[right\])(.*)(\[\/right\])/i',
                '/(\[center\])(.*)(\[\/center\])/i',
                '/(\[left\])(.*)(\[\/left\])/i',
                '/(\\n)/'
                );
                
	 var $HTML = array(
                '<a href="${2}">${4}</a>',
                '<a href="${2}">${2}</a>',
                '<a href="mailto:${2}">${4}</a>',
                '<a href="mailto:${2}">${2}</a>',
                '<fieldset>
                    <legend>${2} Said</legend>
                    ${4}
                </fieldset>',
                '<fieldset>
                    <legend>Quote</legend>
                    ${2}
                </fieldset>',
                '<strong>${2}</strong>',
                '<em>${2}</em>',
                '<u>${2}</u>',
                '<img src="${2}">',
                '<div align="right">${2}</div>',
                '<div align="center">${2}</div>',
                '<div align="left">${2}</div>',
                '<br />'
                );

	function Parse($UserInput)
             {
                
                    $stripped = htmlentities($UserInput);
                
            $UserInput = preg_replace($this->BBCode, $this->HTML, $stripped);
            
                return $UserInput;
                
            }
            
	function UnParse($Input)
            {
            
            $UnParse = preg_replace($this->HTML, $this->BBCode, $Input);
            
                return $UnParse;
                    
            }
    
    }
    
$BBCode = new BBCode;
    
?>

Usage
Just simply use the code below, (example), and that's it, the rest of the code is written for you. :tu:
PHP:
<?php

require 'file_above.php';
$User_Post = $_POST['post'];

echo $BBCode->Parse($User_Post);

?>

Hope it helps you. :)

Adrian

PS: Checkout the tutorial on preg_replace, http://www.dreamit.co.uk/preg_replace-bbcodes-tutorial/
The code is different that i posted in the post, the one above has more functions. :tu:
 
0
•••
hitch i got a question for u..
to make sure users dont use illigal html or php code in forms..i m using "htmlspecialchars" ...if i use the class u gave me..shouldit it also not parse the html code ? because of "htmlspecialchars" ?
 
0
•••
You could use strip_tags() and replacements etc to remove any html tags.
 
0
•••
Look at the function in my code.

htmlentities();

Best method in my opinion, so, you could use my Class, don't worry about other "security" functions.
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back