How to remove ETX character from end of line? (Regular expression or php)

How to remove the ETX character (0x03) from the end of a line? I would like either a function, otherwise I can write a regular expression, all I want to know is how to match the ETX character in a regular expression? trim() does not work.

+8
php regex special-characters control-characters ascii
source share
4 answers

To complete Charles's answer, I had a case where the ETX character was not at the end of the line, so I had to use the following code:

 $string = preg_replace('/\x03/', '', $string); 

Just remove the $ sign.

+8
source share

Have you tried the rtrim function

http://php.net/manual/en/function.rtrim.php

+1
source share

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); } 
+1
source share

When guessing rtrim ($ var, "\ c"); or preg_replace ("/ \ c + \ $ /", '', $ var);

0
source share

All Articles