Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove hidden characters from text string in PHP?

I am having difficulty to match two text strings. One contains some hidden characters from a text string.

I have a text string: "PR & Communications" stored on an SQL database. When pulled from there, into $database_version, var_dump($database_version) reveals the string to have 19 bytes.

I have scraped (with permission) from a website, some text into a variable, $web_version. Ostensibly the string is "PR & Communications" but it does not match the database version, i.e if($database_version == $web_version) is NOT true.

var_dump() reveals $web_version to have 23 bytes. trim() has no effect, nor does strip_tags() but preg_replace( '/[^\PC\s]/u', $web_version ) removes something because afterwards string_var($web_version) reveals the string to comprise 14 bytes only. It has clearly removed something, possibly too much, as the string still does not match with $database_version.

Any ideas how to:

  1. find out what has been removed
  2. strip out just enough to match $database_version?

PS I don't know how to view the variable in hexadecimal code

like image 714
heroicadventures Avatar asked Oct 18 '22 17:10

heroicadventures


1 Answers

$v = preg_replace('/\s+|[[:^print:]]/', '', $string);

trim() removes only " \t\n\r\0\x0B" (see docs), so use snippet above to remove non-printed characters from string.

like image 72
Aleksey Ratnikov Avatar answered Oct 21 '22 05:10

Aleksey Ratnikov