• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

PHP Convert Straight Quotes to Smart Quotes.

Question

Brian Miller    1,638

Using PHP, how would I convert the following:

I "love" Newoin

to the following?

I “love” Newoin

Do you see the use of smart quotes?

 

I can do this in JS but I need to do this in PHP.  In JS, I'd do something like...

$text = 'I "love" Newoin';
console.log($text.replace(/'/g, "’").replace(/\`/g, "’").replace(/\b"/g, "”").replace(/\"/g, "“"));

But I have no idea how to do this in PHP.  Please help. Thanks.

Share this post


Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 1
Neil709    1

Opps, I missed one of the quote types

 

$text = preg_replace(array('/\b"/','/"/',"/'/","/`/"), array('”','“',"’","’"), $text);

 

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
Brian Miller    1,638

"That's a 'magic' sock."    = Incorrect
“That’s a ‘magic’ sock.”    = Correct

Share this post


Link to post
Share on other sites
  • 0
Neil709    1

Hey, try the following code, that should work for you.

 

$text = preg_replace(array('/\b"/','/"/',"/'/"), array('”','“',"’"), $text);

 

Share this post


Link to post
Share on other sites
  • 0
Brian Miller    1,638

THANK YOU!

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.