0

I am trying to convert a weird single speech mark to a normal one in php.

$str = str_replace(chr(039), "'", $str);

I have found it is code 039 from many sources including https://www.atwebresults.com/ascii-codes.php?type=2.

But it causes the "Parse error: Invalid numeric literal" error.

My whole function:

function sanitiseString($str){

$str = str_replace(chr(130), ',', $str);    // baseline single quote
$str = str_replace(chr(132), '"', $str);    // baseline double quote
$str = str_replace(chr(133), '...', $str);  // ellipsis
$str = str_replace(chr(039), "'", $str);    // left single quote                

$str = str_replace(chr(145), "'", $str);    // left single quote
$str = str_replace(chr(146), "'", $str);    // right single quote
$str = str_replace(chr(147), '-', $str);    // double hyphon        
$str = str_replace(chr(150), '-', $str);    // en dash  
$str = str_replace(chr(151), '-', $str);    // em dash  
$str = str_replace(chr(148), '"', $str);    // right double quote                               
$str = str_replace(chr(034), '"', $str);    // weird double speech mark
$str = str_replace(chr(034), '"', $str);    // weird double speech mark

$str = iconv("UTF-8","UTF-8//IGNORE",$str); //ignore everything else unrecognised.

$str = str_replace("’", "'", $str);  
$str = str_replace('“', '"', $str);  
$str = str_replace('”', '"', $str);  

return $str;        
}
Danny Keeley
  • 23
  • 1
  • 1
  • 6
  • Possible duplicate of [Parse error: Invalid numeric literal](https://stackoverflow.com/questions/40735963/parse-error-invalid-numeric-literal) – user3942918 Jul 27 '18 at 08:51

2 Answers2

0

This is because to how integers are handled in PHP7 because numbers starting with 0 are considered as octal values. Octal numbers have a limit of 8 digits per position, from 0 to 7.

Previously in PHP5 octal that contained invalid numbers were truncated. Example: 0239 was taken as 023.

Write the number as string like this should work:

$str = str_replace(chr('0039'), "'", $str);
SiliconMachine
  • 578
  • 1
  • 8
  • 22
  • Thanks for the reply, that either doesn't work or might be referring to a different code; it has no effect on the string returned :/ Specifically I am trying to change this weird left single speech mark ‘ (it is the one when you type on MS Word the ['@] key). – Danny Keeley Jul 27 '18 at 09:07
  • You're welcome. Is working on my side tho, but your code is doing nothing since chr(039) is the same as " ' " therefore, you're replacing a character by the same character. You can check it here: https://3v4l.org/qmc3d. – SiliconMachine Jul 27 '18 at 09:13
0

039 is an invalid value for octal notation, which you invoke with the leading 0. What you want is just chr(39), the 0 is entirely pointless.

Having said that, str_replace(chr(39), "'", $str) won't do anything since chr(39) is the same as "'".

deceze
  • 510,633
  • 85
  • 743
  • 889