Page 1 of 1

Getting rid of bad characters

Posted: Thu Jun 11, 2009 8:25 am
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());
 

Re: Getting rid of bad characters

Posted: Thu Jun 11, 2009 9:26 am
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); 
}

Re: Getting rid of bad characters

Posted: Thu Jun 11, 2009 10:24 am
by lubber123
No - implemented the function but I am still getting the bad characters.

Re: Getting rid of bad characters

Posted: Thu Jun 11, 2009 3:08 pm
by omniuni
Try mysql_escape_string() http://us.php.net/manual/en/function.my ... string.php

Does that help?

Re: Getting rid of bad characters

Posted: Fri Oct 02, 2009 9:56 am
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?

Re: Getting rid of bad characters

Posted: Fri Oct 02, 2009 11:04 am
by omniuni
replace '<br/>' with "\n"

Re: Getting rid of bad characters

Posted: Fri Oct 02, 2009 11:42 am
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?

Re: Getting rid of bad characters

Posted: Fri Oct 02, 2009 12:14 pm
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;
}

Re: Getting rid of bad characters

Posted: Mon Oct 05, 2009 10:39 am
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?

Re: Getting rid of bad characters

Posted: Mon Oct 05, 2009 12:43 pm
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.

Re: Getting rid of bad characters

Posted: Mon Oct 05, 2009 1:23 pm
by lubber123
Okay - so echo '<p><span class="body_text" > <pre>' . htmlentities($row['blog'], ENT_COMPAT, 'UTF-8') . '</pre></span><br></p>';

?

Re: Getting rid of bad characters

Posted: Mon Oct 05, 2009 1:28 pm
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.

Re: Getting rid of bad characters

Posted: Mon Oct 05, 2009 3:38 pm
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>';
?>
 

Re: Getting rid of bad characters

Posted: Mon Oct 05, 2009 3:50 pm
by lubber123
Super - it is looking good now thanks.