Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Applying two PHP functions to a piece of data

    Hello,

    I am trying to apply the stripslashes function to a piece of data that I am retrieving from my database.

    Here is the code I currently have, ... before adding the stripslashes function.

    PHP Code:
    '<p>' substr($row['blog_entry'], 0200).'...</p>' 
    So... my question is .... how would I also apply the stripslashes function to the $row['blog_entry'] as well as keeping the substr function, which is already there.

    Thanks so much if you are able to help.

  • #2
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Would it just be ....

    PHP Code:
    stripslashes(substr($row['blog_entry'], 0200)) 
    ??

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    87
    Thanks
    5
    Thanked 14 Times in 14 Posts
    You don't need to use stripslashes() when getting the string back. Do however use htmlspecialhars() when displaying any text to the user that you don't compose yourself (such as those obtained from the database or from other users). I usually sanitize the text when it goes into the database, for example
    PHP Code:
    $content htmlspecialchars($_POST['blog_entry']); 

  • Users who have thanked Strider64 for this post:

    rhender (04-22-2013)

  • #4
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Oh cool! Thank you.

    So does anyone ever use stripslashes at all?

    or does htmlspecialchars do a better job of cleaning text up? and everyone just uses that instead??

    Thanks again,

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Stripslashes shouldn't be used on data coming out of a database. Without using a prepared statement, you *may* need to do stripslashes on input data IF magic_quotes_gpc is in use. Otherwise you will have escaped data stored within the db.
    Stripslashes would go directly around the argument pulled though, not that of any other manipulations to the string:
    PHP Code:
     '<p>' substr(stripslashes($row['blog_entry']), 0200).'...</p>' 
    With a substr, without it you would chop off up to 200 characters *including* the escapes (which should be taken care of before insert). The intent would be to have 200 characters *excluding* the escapes.

    You would use htmlenties or htmlspecialchars if you want to interpret the string as text and not as html. To say it is something you should do is misleading; it is what you should do if the string *shouldn't* contain HTML that is parsable. If you store templates within a database, than you wouldn't want to convert it with htmlentities or htmlspecialchars.

    Edit:
    My bad, I missed that Strider indicated to do it when it is something you don't compose yourself. That would be my thoughts on that as well (you'd write the templates; user input shouldn't allow it unless you want to do that [which you typically don't])
    Last edited by Fou-Lu; 04-23-2013 at 12:19 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    rhender (04-23-2013)

  • #6
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you again Fou-Lu!

    I really love this forum. I was able to correct my problem with your guys' help.

    Sometimes I have to read through these responses a few times though.. to learn from them.

  • #7
    Regular Coder
    Join Date
    May 2012
    Location
    USA
    Posts
    102
    Thanks
    0
    Thanked 7 Times in 7 Posts
    I agree with rhender, as the code shared by him is perfect.

  • Users who have thanked annaharris for this post:

    rhender (04-23-2013)

  • #8
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks for your input Anna. Yeah, that ended up working for me.

    By having the stripslashes function on the outside and the substr function on the inside... that was what worked.

    But everyone helped me get to that conclusion.. great forum.

    ONE MORE QUESTION IF ANYONE CARES TO HELP / ANSWER:

    How would I keep using the substr function (I'm using it to only show an excerpt of the blog post) - but instead of having it cut off at exactly 200 characters... have it cut off after 200 characters - BUT the last word of the excerpt has to be completed - so that words don't get cut off in the middle... does that make sense?? thanks again,

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    There are a few ways to get there:
    PHP Code:
    $short array_shift(explode(PHP_EOLwordwrap($s$iChrsPHP_EOLfalse))); 
    Is probably the easiest where $s is the string, $iChrs is the number of characters give or take, and $short is the cut string to the next shortest length.
    You can also do it with substr, but it takes more work since you have to deal with potentially shorter strings, false results, etc etc.
    PHP Code:
    $iChrs += strlen($s) > $iChrs ? (($c strpos($s' '$iChrs 1)) ? ($c $iChrs) : 0) : 0;
    print 
    substr($s0$iChrs); 
    Assuming a word ends on a space.
    I can probably come up a with a few other ideas as well. I'm sure I've done this simpler than this whilst still using substr in the past.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    rhender (04-24-2013)

  • #10
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Wow, thank you again Fou-Lu.

    I will have to study this code to get a better understanding.

    Terms for me to research on this...

    >> PHP_EOL
    >> array_shift
    >> wordwrap

    Exciting...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •