jasonc310771 Posted December 15, 2022 Share Posted December 15, 2022 $string = "t*e*s*t"; $pattern = "/[^A-Za-zÀ-ÿ0-9\-\_\(\)\[\]\{\} ]/"; // keep all language letters, numbers, all types of brackets, spaces, hyphen, dash fullstop $cleanStr = preg_replace($pattern, '', $string); echo("{" . $cleanStr . "}"); I do not understand why this is not working, apart from the pattern which I know still need correcting the code is the same as code I used ages ago. Apart from the string. I get this error... Parse error: syntax error, unexpected '''' (T_CONSTANT_ENCAPSED_STRING) I am trying to remove all special characters from a string apart from all letters and accented, numbers and the usual characters allowed in a file name. Quote Link to comment https://forums.phpfreaks.com/topic/315647-preg_replace-remove-special-characters/ Share on other sites More sharing options...
requinix Posted December 15, 2022 Share Posted December 15, 2022 Make sure you're using an editor which is not inserting obnoxious characters into your code. Such as non-breaking spaces, which I see a few of in there. Quote Link to comment https://forums.phpfreaks.com/topic/315647-preg_replace-remove-special-characters/#findComment-1603553 Share on other sites More sharing options...
jasonc310771 Posted December 15, 2022 Author Share Posted December 15, 2022 even using the original code i got from a website it still does not work, same error. Is it the code or the pattern, both ? Quote $string = "t*e*s*t"; $pattern = "/[^A-Za-z0-9 ]/"; // keep all language letters, numbers, all types of brackets, spaces, hyphen, dash fullstop //$pattern = "/[^A-Za-zÀ-ÿ0-9\-\_\(\)\[\]\{\} ]/"; // keep all language letters, numbers, all types of brackets, spaces, hyphen, dash fullstop $cleanStr = preg_replace($pattern, '', $string); echo("{" . $cleanStr . "}"); Quote Link to comment https://forums.phpfreaks.com/topic/315647-preg_replace-remove-special-characters/#findComment-1603554 Share on other sites More sharing options...
requinix Posted December 15, 2022 Share Posted December 15, 2022 PHP doesn't see non-breaking spaces as whitespace. So it gets confused while parsing and will spit out seemingly random error messages. Quote Link to comment https://forums.phpfreaks.com/topic/315647-preg_replace-remove-special-characters/#findComment-1603556 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.