How do you remove Apostropies and Double Quotes?

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

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you remove Apostropies and Double Quotes?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: How do you remove Apostropies and Double Quotes?

Post 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); 
} 
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: How do you remove Apostropies and Double Quotes?

Post 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>
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you remove Apostropies and Double Quotes?

Post 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.

:?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: How do you remove Apostropies and Double Quotes?

Post 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>
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you remove Apostropies and Double Quotes?

Post 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>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: How do you remove Apostropies and Double Quotes?

Post by Weirdan »

simonmlewis wrote:Since it is ALL within the PHP code
Now this is a problem you have to fix :D
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you remove Apostropies and Double Quotes?

Post by simonmlewis »

Well that's why I am here - I can't!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: How do you remove Apostropies and Double Quotes?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you remove Apostropies and Double Quotes?

Post 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
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: How do you remove Apostropies and Double Quotes?

Post 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>';

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you remove Apostropies and Double Quotes?

Post 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)
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How do you remove Apostropies and Double Quotes?

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: How do you remove Apostropies and Double Quotes?

Post 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!
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you remove Apostropies and Double Quotes?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply