Change apostrophe to ’

I need to search a string for apostrophes and change them into the html code: ’
I thought the following code would work, but it doesn't seem to have any effect.

$item1 = str_replace("'", "’", $item1);


Comments

  • I'm not sure, this seems to me like more PHP 4.0 quirkiness. I ran the following code to test it:

    [code]
    <?php
    $item1 = "single 'quotes' need to be 'stripped'..";

    echo $item1 . "
    ";
    $item1 = str_replace("'", "&#146;", $item1);
    echo $item1;
    ?>
    [/code]

    And got the following output:

    [code]
    single 'quotes' need to be 'stripped'..
    single &#146;quotes&#146; need to be &#146;stripped&#146;..
    [/code]

    So ... it looks like it should work. What's happening when you run that same code?

    T

    : I need to search a string for apostrophes and change them into the html code: &#146;
    : I thought the following code would work, but it doesn't seem to have any effect.
    :
    : $item1 = str_replace("'", "&#146;", $item1);
    :
    :
    :

  • I appreciate it but I found a working solution:

    $item1 = preg_replace("/'/","&#146;",$item1);


    This replaces any apostrophes with the numerical code and stores it as such. Therefore when used in a javascript single quote variable assignment, it assigns the code and not the character. I wish I could say I came up with the solution, but alas once again someone smarter than me had to give it to me. Oh well. One day maybe I'll be giving the answers instead of asking the questions. THANKS!
  • Hm. I'm still a little curious why your original solution didn't work. preg_replace is essentially the same as str_replace, except it takes a regex in the first argument instead of a plain old string. As a result, preg_replace actually incurs a little more overhead than str_replace does.

    Out of curiosity I threw together a test script and after iterating 10,000 times through preg_replace and str_replace, the difference was only about .001 so I'd say the difference won't matter much to you.

    Anyhow, glad you got it working =)

    T

    : I appreciate it but I found a working solution:
    :
    : $item1 = preg_replace("/'/","&#146;",$item1);
    :
    :
    : This replaces any apostrophes with the numerical code and stores it as such. Therefore when used in a javascript single quote variable assignment, it assigns the code and not the character. I wish I could say I came up with the solution, but alas once again someone smarter than me had to give it to me. Oh well. One day maybe I'll be giving the answers instead of asking the questions. THANKS!
    :

  • I think my first solution didn't work because I used ' instead of /'/ in the replace statement.
  • : I think my first solution didn't work because I used ' instead of /'/ in the replace statement.
    :

    The // notation is used for regexes. str_replace doesn't use regexes so the surrounding /s shouldn't be there. preg_replace does use regexes so the / / are required.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories