-1

I have the following string:

LONDON — Britain’s unemployment rate held steady in the three months through January, reinforcing the Bank of England’s case for keeping interest rates at a record low.

If I use something like htmlentities I get the following string:

LONDON — Britain’s unemployment rate held steady in the three months through January, reinforcing the Bank of England’s case for keeping interest rates at a record low.

However I want to get WINDOWS-1252 entities instead such as:

LONDON — Britain’s unemployment rate held steady in the three months through January, reinforcing the Bank of England’s case for keeping interest rates at a record low.

I've tried using different functions such as mb_convert_encoding, iconv, get_html_translation_table but I'm unable to find a way to do this.

Any help would be appreciated.

bigmike7801
  • 3,908
  • 9
  • 49
  • 77

1 Answers1

0

Basically the conversion is the &#ASCII; in format. So using the ascii value of those's special characters, you can easily implement it in preg_replace_callback function. Take a look at the following example:

$text = <<<EOT
LONDON — Britain’s unemployment rate held steady in the three months through January, reinforcing the Bank of England’s case for keeping interest rates at a record low.
EOT;

$text = preg_replace_callback(
    "/([—’])/",   // <-- place characters inside the [] if you need to change
    function ($m){ return "&#".ord($m[1]).";"; },
    $text
);

print "$text\n";

I have just used two(—’) characters here inside []. if you need more, just add those one by one.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • Unfortunately that seems like it added too much encoding. Wen running your code I get the following `LONDON — Britain’s unemployment rate held steady in the three months through January, reinforcing the Bank of England’s case for keeping interest rates at a record low.` – bigmike7801 Mar 23 '14 at 22:26