academe / SerializeParser

A PHP parser for serialized data, to be able to "peek" into serialize strings.
MIT License
4 stars 2 forks source link

Strip quotes #4

Closed jj5 closed 7 years ago

jj5 commented 7 years ago

Hey. Try stripQuotes like this:

public function strip_quotes( $string ) {

  if ( $string === '' ) { return ''; }

  if ( $string[ 0 ] === '"' ) { $string = substr( $string, 1 ); }

  $end = strlen( $string ) - 1;

  if ( $string[ $end ] === '"' ) { $string = substr( $string, 0, $end ); }

  return $string;

}
judgej commented 7 years ago

I've used a preg_replace for now. The above is probably more efficient (would be fun to quantify), and can be revisited later.

One check that could be done above is the string length - just return $string if its length is less than 2, since the minimum length of a quoted string is 2 - just two quotes.

The check I would do here is to see if the first byte is " and then if the last byte is a ". If both are true, then return (something like) substr($string, 1, -1).

judgej commented 7 years ago

Got rid of the preg_replace, which should speed things up a bit.