user2075625
user2075625

Reputation: 103

PHP: How to convert smart quotes to normal quotes when regex matching?

I have a small piece of code that searches a string for a pattern of <quote>text<quote> - The idea here being to find quotes within a string.

Currently I have this as the preg_match function

preg_match_all('/"([^"]+)"/', $essay, $q);

However I have found that when pasting in text from word processors or PDFs - the quotation mark becomes “ ” a rich text format quotation mark.

How could you go about filtering these out and converting them back to plain text quotation marks?

Upvotes: 0

Views: 893

Answers (1)

Stoic
Stoic

Reputation: 10754

You can simply use str_replace() function to convert these smart quotes to normal quotes, like this:

$essay = str_replace(array('“','”'), '"', $essay);
preg_match_all('/"([^"]+)"/', $essay, $q);

Upvotes: 1

Related Questions