ETX is a control symbol, which means that we have to take extra efforts to create it.
In the PHP interactive prompt:
php > $borked = "Hello, have an ETX:" . chr(3); php > print_r($borked); Hello, have an ETX: php > var_export($borked); 'Hello, have an ETX:' php > echo urlencode($borked); Hello%2C+have+an+ETX%3A%03 php > var_dump( preg_match('/' . chr(3) . '/', $borked) ); int(1) php > var_dump( preg_match('/' . chr(3) . '$/', $borked) ); int(1) php > echo urlencode( preg_replace('/' . chr(3) . '$/', '', $borked) ); Hello%2C+have+an+ETX%3A
In other words, you can simply insert a character into the regular expression string. Now this can be unpleasant. This method can be unpleasant based on character sets and other terrible things.
In addition to chr(3) you can also try urldecode('%03') to create a character. You can also use special escape syntax, as indicated in the PCRE syntax : /\x03$/
Here is the function for you using this last method:
function strip_etx_at_end_of_string($string) { return preg_replace('/\x03$/', '', $string); }
Charles
source share