Page 1 of 2

How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 12:14 pm
by simonmlewis
I am trying to just render the result of a field in a table, but because people have entered " or ' into the table, the render goes a bit wrong.

I thought I could use this to remove the quotes:

Code: Select all

    $string = ereg_replace(""", "",$row->communication);
I also tries magic quotes, but I think that's more for putting them back.

Can anyone help me with this please? I've done it before, but the code just wont work.

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 12:34 pm
by Reviresco
This does smart quotes plus hyphen. Probably could also use the ellipsis (...).

Code: Select all

function convert_smart_quotes($string) 
{ 
    $search = array(chr(145), 
                    chr(146), 
                    chr(147), 
                    chr(148), 
                    chr(151)); 
 
    $replace = array("'", 
                     "'", 
                     '"', 
                     '"', 
                     '-'); //hyphen, could also be –
 
    return str_replace($search, $replace, $string); 
} 

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 12:48 pm
by Weirdan
simonmlewis wrote: How do you remove Apostropies and Double Quotes?
I assume people entered them for a reason and display them, applying escaping where necessary.
simonmlewis wrote: I am trying to just render the result of a field in a table, but because people have entered " or ' into the table, the render goes a bit wrong.

Code: Select all

<div>
<?php echo htmlspecialchars($string, ENT_QUOTES, "UTF-8"); ?>
</div>

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 12:56 pm
by simonmlewis

Code: Select all

<td valign='top'><a href='index.php?page=emailid&id=$row->id&head=email detail' style='text-decoration: none' title='";

htmlspecialchars($row->communication, ENT_QUOTES, "UTF-8");

echo ">$row->reason</a></td>
This doesn't work. Maybe I have adjusted it wrongly.

Thanks for the Javascript version, but I have no idea how to implement that.
I thought it was some kind of Magic Quotes or Strip type code that is used. The htmlspecialchars looked familiar but nothing here is working for me.

:?

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 1:03 pm
by Weirdan
You're doing something very weird in your html/php code... I assume it should look like this:

Code: Select all

<td valign='top'><a href='index.php?page=emailid&id=<?php echo htmlspecialchars($row->id, ENT_QUOTES, "UTF-8");?>&head=email%20detail' style='text-decoration: none' title='<?php echo htmlspecialchars($row->communication, ENT_QUOTES, "UTF-8");?>'><?php echo htmlspecialchars($row->reason, ENT_QUOTES, "UTF-8");?></a></td>

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 1:11 pm
by simonmlewis
Since it is ALL within the PHP code, it's like this at the moment, but doesn't work

Code: Select all

<td valign='top'><a href='index.php?page=emailid&id=$row->id&head=email detail' style='text-decoration: none' title='htmlspecialchars($row->communication, ENT_QUOTES, "UTF-8");>$row->reason</a></td>
<td valign='top' nowrap><a href='index.php?page=emailid&id=$row->id&head=email detail' style='text-decoration: none'>$row->website</a>&nbsp;&nbsp;</td>

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 1:36 pm
by Weirdan
simonmlewis wrote:Since it is ALL within the PHP code
Now this is a problem you have to fix :D

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 2:00 pm
by simonmlewis
Well that's why I am here - I can't!

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 2:21 pm
by Reviresco
Okay I misread this post the first time I replied -- I thought you were asking how to get rid of curly quotes.

Since you said the code was included in PHP code, I'm assuming you are echoing it? If so you need to pay attention to the quotes and apostrophes you use in the code. Here's one way to do it:

Code: Select all

echo '
<td valign="top"><a href="index.php?page=emailid&id=' . $row->id . '&head=email detail" style="text-decoration: none;" title="' . htmlspecialchars($row->communication, ENT_QUOTES, "UTF-8") . '">' . $row->reason . '</a></td>
<td valign="top" nowrap><a href="index.php?page=emailid&id=' . $row->id . '&head=email detail" style="text-decoration: none">' . $row->website . '</a>&nbsp;&nbsp;</td>';
But also I'm uncertain if you're tryng to actually remove the quotes, escape them, or convert them to html characters.

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 2:35 pm
by simonmlewis
Based on that, I have tried this:

Code: Select all

echo '<td valign="top"><a href="index.php?page=emailid&id=$row->id&head=email detail" style="text-decoration: none" title=' . htmlspecialchars($row->communication, ENT_QUOTES, "UTF-8") . '>$row->reason</a></td>';
But all it does is render:
$row->reason

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 3:03 pm
by Reviresco

Code: Select all


echo '<td valign="top"><a href="index.php?page=emailid&id=' . $row->id . '&head=email detail" style="text-decoration: none;" title="' . htmlspecialchars($row->communication, ENT_QUOTES, "UTF-8") . '">' . $row->reason . '</a></td>';


Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 3:12 pm
by simonmlewis
Perfect - thanks.
I couldn't see why I would have to put the $row->reason inside the dots. Still can't really. And I thought I knew my PHP!! lol

8)

Re: How do you remove Apostropies and Double Quotes?

Posted: Thu May 13, 2010 3:45 pm
by AbraCadaver
simonmlewis wrote:Perfect - thanks.
I couldn't see why I would have to put the $row->reason inside the dots. Still can't really. And I thought I knew my PHP!! lol

8)
It's not inside the dots! It's OUTSIDE of the single quotes. This is the most basic of PHP, so better get reading:

http://www.php.net/manual/en/language.types.string.php

Re: How do you remove Apostropies and Double Quotes?

Posted: Fri May 14, 2010 2:48 am
by hypedupdawg
simonmlewis wrote:I couldn't see why I would have to put the $row->reason inside the dots.
The dots anctually stand for the concatenation function in PHP - that is, adding one or more string together. If you write a variable inside a string and echo it like this:

Code: Select all

<?php $varA = "this text";
echo "Echoing $varA"; ?>
It will simply output the one, literal string: "Echoing $varA"

However, if you add the concatenation fuction like so:

Code: Select all

<?php $varA = "this text";
echo "Echoing " . $varA; ?>
It will render the value of the variable first, then join both strings together and output: "Echoing this text"


Hope this helps!

Re: How do you remove Apostropies and Double Quotes?

Posted: Fri May 14, 2010 2:54 am
by simonmlewis
I beg to differ.
<?php $name = "Johnny";
echo "My name is $name";
?>

Produces.... "My name is Johnny".

Isn't that one of the basics of PHP? Hence why I had the query about the dots.