Adding stripslashes

Hi, I am trying to add the function stripslashes to the below code:

<?php echo strlen($product['description']) > 90 ? substr($product['description'], 0, 90) . '...' : $product['description']; ?>

Not sure where to place it, tried may variations but stuck - any help would be great.

thank you.

Then use apache level error redirects to an error handling file.

I understand and share the concern, but I’d rather run the risk that an error might leak out and be logged by setting error reporting that high then have errors occur unlogged with a mysterious white screen being presented to the user.

the examples in the php manual should help to show you how to use stripslashes()

Have your tried something like this?

if ( get_magic_quotes_gpc() ) { 
    $product = stripslashes($product);
}

May not be relevant, as I know very little PHP, but worth a shot.

What do you want to achieve with the stripslashes?

I see quite a few places where you could put it. Some aren’t as useful as others though :wink:

When adding a product description I am just using a plain text field - if you write a word “you’ll” the ’ (apostrophe) causes a mess so I want to cut it out.

How come the backslash is in the DB in the first place? That shouldn’t happen.

because I am not doing anything fancy - is is the most basic way of doing it :slight_smile:

is there a way to incorporate the stripslashes in this line?

 <?php echo strlen($product['description']) > 90 ? substr($product['description'], 0, 90) . '...' : $product['description']; ?>

I thought it would of went at the beginning but did not work.

cheers

Well if you were to put it anywhere I’d do it like this:


<?php
echo
  stipslashes(
    strlen($product['description']) > 90
      ? substr($product['description'], 0, 90) . '...'
      : $product['description']
  );
?>

But I still don’t see why there are slashes in the DB. There shouldn’t be …

causes a mess where and how?

if I have a table column of type text and I run

 
INSERT INTO tblperson (fldFamilyName) VALUES ("o'reilly")

I see a value of o’reilly in the database table.

you can then use htmlentities() to display data with special characters in it in your html when you extract the data from the database.

Guess I am a dur dur ! ha - it’s ok guys, appreciate the comments.

If your string is getting the slashes from the magic_quotes mode of PHP then, if possible, turn magic quotes off. This can be done from the .htaccess file or PHP.ini (better to use the ini file if you have access to it). Indeed, I recommend the following .htaccess code for any PHP deployment.


	php_flag register_globals off		
	php_flag magic_quotes_gpc off		
	php_flag display_startup_errors on	
	php_flag display_errors on
	php_value error_reporting -1

Notices and strict warnings are annoying, but addressing them makes you a better programmer. In any event using these settings is a lot easier than having to run strip_slashes and risk accidental removal of slashes that should be there. And turning register globals off is much easier than trying to remove the dangerous effects of having the setting on.

Reminder Register globals and magic quotes are deprecated and WILL BE REMOVED in PHP 6. So the instructions to turn them off above simulate the environment the next version of PHP will enforce nicely.

Are you sure about that? :shifty:

[fphp]set_error_handler[/fphp] was made for a reason. At the very least these settings should be used in development.

In development yes, but I wouldn’t want to see the error messages generated by PHP on a production website :slight_smile:

Fair enough :slight_smile: