replacing a single quotation mark in a string
Results 1 to 4 of 4

Thread: replacing a single quotation mark in a string

  1. #1
    Member
    Join Date
    Nov 2016
    Posts
    62

    replacing a single quotation mark in a string

    I have

    $mystring = "Bill's components";

    I want to get rid of the ' and replace it with a space (hopefully giving $mystring="Bill s components") so I wrote

    $mystring = str_replace("'" , " " , $mystring);

    but this doesn't work; the ' remains in the string.

    What to do, please ?

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,807
    Is there any possibility the quote you want to replace is not a basic "straight" quote, but a "directional" quote? (Like what MS Word will change it to automatically by default?)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,807
    PS: Your code works fine for me:
    Code:
    php > $myString = "Bill's Components";
    php > $myString = str_replace("'", " ", $myString);
    php > echo $myString;
    Bill s Components
    php > quit
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    Senior Member
    Join Date
    Apr 2003
    Location
    Flanders Fields
    Posts
    5,833
    Quote Originally Posted by NogDog View Post
    Is there any possibility the quote you want to replace is not a basic "straight" quote, but a "directional" quote? (Like what MS Word will change it to automatically by default?)
    Some people also call them 'curly quotes.' The basic idea is that it's not the basic single quote mark (or apostrophe) key on your keyboard which is
    Code:
    '
    which has ASCII code 39.

    Some info here might help spell out the differences a bit.

    How to effectively replace curly quotes sort of depends on your data source's charset encoding. Is it Latin-1? UTF-8?
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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