Getting rid of bad characters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Getting rid of bad characters

Post by lubber123 »

I have a simple form that accepts data into a textfield and then enters it into a database. When it adds it to the db it converts quotes to â€. I have a function (mysql_prep) to check for magic_quotes or to apply mysqli_real_escape_string(). I tried to addslashes(). It still converts the quotes. Can anyone tell me how to stop this? Below is the code I have:

Code: Select all

 
if((isset($_REQUEST['blog'])) && ($_REQUEST['blog'] != "")){
                    $_REQUEST['blog'] = nl2br($_REQUEST['blog']);
                    $_REQUEST['blog'] = mysql_prep($_REQUEST['blog']);
                }
                                        
                //inserting into the db
                $todate = date("Y-m-d");
                echo $query = "INSERT INTO DB(date, blog 
                                )VALUES(
                            '{$todate}', '{$_REQUEST['blog']}')";
                $result = mysql_query($query, $connection) or die(mysql_error());
 
Last edited by Benjamin on Thu Jun 11, 2009 7:15 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Getting rid of bad characters

Post by Reviresco »

This function from http://shiflett.org/blog/2005/oct/conve ... s-with-php looks good:

Code: Select all

function convert_smart_quotes($string) 
{ 
    $search = array(chr(145), 
                    chr(146), 
                    chr(147), 
                    chr(148), 
                    chr(151)); 
 
    $replace = array("'", 
                     "'", 
                     '"', 
                     '"', 
                     '-'); 
 
    return str_replace($search, $replace, $string); 
}
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: Getting rid of bad characters

Post by lubber123 »

No - implemented the function but I am still getting the bad characters.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Getting rid of bad characters

Post by omniuni »

Try mysql_escape_string() http://us.php.net/manual/en/function.my ... string.php

Does that help?
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: Getting rid of bad characters

Post by lubber123 »

Ah, I've just been made aware of the fact that now the text outputs the actual html and instead of having paragraph breaks it shows "<br >." How do I still implement the html code but do away with the special characters? Is there a way to str_replace the <br> with a line break?
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Getting rid of bad characters

Post by omniuni »

replace '<br/>' with "\n"
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: Getting rid of bad characters

Post by lubber123 »

I am able to change all the other characters - e.g. "w" with "e" - but it doesn't want to change the "<br />" - will it not change html?
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Getting rid of bad characters

Post by omniuni »

hmmm....

Code: Select all

function br2nl($stringTobr2nl){
//returns a String where HTML line breaks have been converted to new lines.
//to Use: echo br2nl('my super <br/> broken string');
//or: $myFixedString=br2nl('my super <br/> broken string');
$brStrings[] = '<br>';
$brStrings[] = '<br/>';
$brStrings[] = '<br />';
$brStrings[] = '<br/ >';
$br2nl = str_replace($brStrings, "\r\n", $stringTobr2nl);
return $br2nl;
}
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: Getting rid of bad characters

Post by lubber123 »

Okay, so the html breaks "<br />" are gone but it did not replace the line breaks. So, now we have one big paragraph - no line breaks. In the code you put \r\n in exchange for <br /> - is that the correct line break?
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Getting rid of bad characters

Post by omniuni »

"\r\n" covers both Windows and Linux encoding for line breaks. Remember, though, that this only shows in the source code. If you want this to be viewable in HTML displayed in the browser, you need to put <pre> tags around it.
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: Getting rid of bad characters

Post by lubber123 »

Okay - so echo '<p><span class="body_text" > <pre>' . htmlentities($row['blog'], ENT_COMPAT, 'UTF-8') . '</pre></span><br></p>';

?
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: Getting rid of bad characters

Post by lubber123 »

Still trying to get my head around this formatting. It seems everything I do makes another weird look to the page. It solves the last issue and then creates another.

Adding the <pre> tag added the line breaks but formatted as below: -- Weird line spacing.



The "circle of life" is filled with change; it is something everybody can count on experiencing.

At this moment we are experiencing seasonal changes in Virginia: Summer into Fall and Fall into Winter. Our lives consist of ongoing change.

We experience weather changes, economic change, physical changes, geographical change -- and, of course, political change.



Throw into the mix of these changes the fact that we change as we get older (and, hopefully, a little wiser), and it is small wonder that people feel confused, overwhelmed, and even a little lost.

Much like the flotsam and jetsam on the high seas, many are adrift on life's sea, and are searching for substance and a sense of permanency.



The truth is that THE ONLY CONSTANT IN AN EVER-CHANGING WORLD IS GOD.



Despite teachings to the contrary God is never angry at you. He loves you. He is Love, and Love "understands all things" -- even you and me.



God does not withhold blessing from us because yesterday that old, besetting weakness in our life won the battle of the day.

God never has a "blue Monday" or a "bad week".

He loves you and me with a love that does not waiver, never changes to any degree, nor does it stop because we stopped our obedient following of Him.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Getting rid of bad characters

Post by omniuni »

Hmmm... I think less is more here.

Instead of

Code: Select all

echo '<p><span class="body_text" > <pre>' . htmlentities($row['blog'], ENT_COMPAT, 'UTF-8') . '</pre></span><br></p>';
try

Code: Select all

 
<?php
echo '<p><span class="body_text" >' . nl2br(htmlentities($row['blog'], ENT_COMPAT, 'UTF-8')) . '</span></p>';
?>
 
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: Getting rid of bad characters

Post by lubber123 »

Super - it is looking good now thanks.
Post Reply