SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: str_replace

  1. #1
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    str_replace

    Hi there.

    I am trying to use str_replace to strip a variable of it's apostrophes (single quote marks) but for some reason it isn't working.

    This is the original variable:

    PHP Code:
        $post_title mysql_result($query_result$i"post_title"); 
    If you look at the page http://ivegotkids.com/newsletters/aries-daily.php you will see under the "Recent Articles" section there are some off characters where it says Top three �yummy mummy� swimsuits. This is because the original post title in the database is Top three 'yummy mummy' swimsuits.

    To avoid this happening - I want to remove the apostrophes from the returned value of the variable.

    I have tried this:

    PHP Code:
    $post_title mysql_result($query_result$i"post_title");
    $post_title str_replace("'"" "$post_title); 
    But it has no effect.

    I have tried it like this:

    PHP Code:
    $post_title mysql_result($query_result$i"post_title");
    $post_title str_replace("\'/"" "$post_title); 
    Also, no effect.

    I have even tried it with preg_replace but that doesn't work either.

    So what is the correct way to strip all apostraphes from the $post_title variable?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,087
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    They aren't regular single quotes, but the "fancy" ones ‘ (U+2018) and ’ (U+2019)
    Search and replace those and you'll be fine

    http://en.wikipedia.org/wiki/Quotation_mark_glyphs
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,101
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    $s = str_replace('\'', '', $s);

    single quote, backslash, single quote, single quote
    What I lack in acuracy I make up for in misteaks

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    They aren't regular single quotes, but the "fancy" ones ‘ (U+2018) and ’ (U+2019)
    Search and replace those and you'll be fine

    http://en.wikipedia.org/wiki/Quotation_mark_glyphs
    Hi there,

    I see what you mean - Unfortunately, though, it doesn't work. I have tried:

    PHP Code:
        $post_title mysql_result($query_result$i"post_title");
        
    $post_title str_replace('’'''$post_title);
        
    $post_title str_replace('‘'''$post_title); 
    And nothing changes. I have also tried just one of the replacements on it's own in case two together didn't work like so:

    PHP Code:
        $post_title mysql_result($query_result$i"post_title");
        
    $post_title str_replace('’'''$post_title); 
    But this produced no change either.

    Any other ideas?

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lorenw View Post
    $s = str_replace('\'', '', $s);

    single quote, backslash, single quote, single quote
    Sorry - that did not work either.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,087
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You can't use the htmlentities, but should use the uni codes instead.

    PHP Code:
    $post_title str_replace(array(chr(2018), chr(2019)), ''$post_title); 
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    You can't use the htmlentities, but should use the uni codes instead.

    PHP Code:
    $post_title str_replace(array(chr(2018), chr(2019)), ''$post_title); 
    Thanks for the reply - Unfortunately, that has not worked either.

    This is my code now:

    PHP Code:
     $post_title mysql_result($query_result$i"post_title");
        
    $post_title str_replace(array(chr(2018), chr(2019)), ''$post_title); 
    When I echo the post_title it still comes up with Top three �yummy mummy� swimsuits

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,087
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Would it be possible to just edit that title? Probably easier than trying to pursuit this route ...
    If not, run the following code to see what character codes the title actually uses:

    PHP Code:
    for ($i=0$i<strlen($post_title); $i++) {
        
    $char substr($post_title$i1);
        echo 
    '('.$char.'='.ord($char).') ';

    Then take the codes it outputs and use those in str_replace in combination with chr().
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Would it be possible to just edit that title? Probably easier than trying to pursuit this route ...
    If not, run the following code to see what character codes the title actually uses:

    PHP Code:
    for ($i=0$i<strlen($post_title); $i++) {
        
    $char substr($post_title$i1);
        echo 
    '('.$char.'='.ord($char).') ';

    Then take the codes it outputs and use those in str_replace in combination with chr().
    Hi, it is possible to change the post title for that particular post and I have done this now since I couldn't get either of the previous solutions to work. However, I'm not happy with just leaving it at that because that means it could happen again if any of our site's editors forget not to copy and paste from MS Word without replacing the punctuation. So I'll keep a hold of this latest piece of code you've given me and run a few tests on our development site and hopefully catch any future mixups before they happen

    Thanks for your help.


Bookmarks

Posting Permissions

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