Cara menggunakan php escape spaces

Tip: To convert special HTML entities back to characters, use the htmlspecialchars_decode() function.

Show

Syntax

htmlspecialchars(string,flags,character-set,double_encode)

Parameter Values

ParameterDescriptionstringRequired. Specifies the string to convertflagsOptional. Specifies how to handle quotes, invalid encoding and the used document type.

The available quote styles are:

  • ENT_COMPAT - Default. Encodes only double quotes
  • ENT_QUOTES - Encodes double and single quotes
  • ENT_NOQUOTES - Does not encode any quotes

Invalid encoding:

  • ENT_IGNORE - Ignores invalid encoding instead of having the function return an empty string. Should be avoided, as it may have security implications.
  • ENT_SUBSTITUTE - Replaces invalid encoding for a specified character set with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; instead of returning an empty string.
  • ENT_DISALLOWED - Replaces code points that are invalid in the specified doctype with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD;

Additional flags for specifying the used doctype:

  • ENT_HTML401 - Default. Handle code as HTML 4.01
  • ENT_HTML5 - Handle code as HTML 5
  • ENT_XML1 - Handle code as XML 1
  • ENT_XHTML - Handle code as XHTML
character-setOptional. A string that specifies which character-set to use.

Allowed values are:

  • UTF-8 - Default. ASCII compatible multi-byte 8-bit Unicode
  • ISO-8859-1 - Western European
  • ISO-8859-15 - Western European (adds the Euro sign + French and Finnish letters missing in ISO-8859-1)
  • cp866 - DOS-specific Cyrillic charset
  • cp1251 - Windows-specific Cyrillic charset
  • cp1252 - Windows specific charset for Western European
  • KOI8-R - Russian
  • BIG5 - Traditional Chinese, mainly used in Taiwan
  • GB2312 - Simplified Chinese, national standard character set
  • BIG5-HKSCS - Big5 with Hong Kong extensions
  • Shift_JIS - Japanese
  • EUC-JP - Japanese
  • MacRoman - Character-set that was used by Mac OS

Note: Unrecognized character-sets will be ignored and replaced by ISO-8859-1 in versions prior to PHP 5.4. As of PHP 5.4, it will be ignored an replaced by UTF-8.

double_encodeOptional. A boolean value that specifies whether to encode existing html entities or not.
  • TRUE - Default. Will convert everything
  • FALSE - Will not encode existing html entities


Technical Details

Return Value:Returns the converted string

If the string contains invalid encoding, it will return an empty string, unless either the ENT_IGNORE or ENT_SUBSTITUTE flags are set

I've tried to cover the key 
3
50 functions and methods here. You should definitely take a look at the documentation to read about the functions not covered in the tutorial. If you have any questions, feel free to let me know in the comments.

saya agak bingung. mohon koreksi kalau saya salah paham. maksud pertanyaannya itu gini: di database sudah ada nama "contoh misalnya" kemudian user coba mendaftar lagi dengan nama "contoh [space yang banyak] misalnya". maunya agan data nama tersebut tidak boleh masuk ke database karena sudah ada nama sebelumnya, yang membedakan cuma jumlah spasinya. Kalau seperti itu kayaknya kodenya harus dirubah. karena variable nama yang agan oper tersebut belum ditrim. coba cek data agan: pada file controller.php $nama = $_POST['username']; (tidak ada validasi untuk menghilangkan spasi dengan fungsi trim()) pada file model.php $nama = $this->escape($nama); (hanya escape saja tidak ada trim()); sehingga variable nama yang belum ditrim tersebut jelas akan masuk ke database. jadinya tidak muncul peringatan. sedangkan pada fungsi if(!empty(trim($nama)) itu hanya syarat saja tapi trim($nama)-nya tidak agan oper. coba kasi ini gan dibawah $nama = $_POST['username'] : $nama = trim(preg_replace('/\s+/',' ', $nama));

avatar onirusama

@onirusama

265 Kontribusi 183 Poin

Dipost: 6 tahun yang lalu Update 6 tahun yang lalu

Jawaban

saya agak bingung. mohon koreksi kalau saya salah paham. maksud pertanyaannya itu gini: di database sudah ada nama "contoh misalnya" kemudian user coba mendaftar lagi dengan nama "contoh [space yang banyak] misalnya". maunya agan data nama tersebut tidak boleh masuk ke database karena sudah ada nama sebelumnya, yang membedakan cuma jumlah spasinya. Kalau seperti itu kayaknya kodenya harus dirubah. karena variable nama yang agan oper tersebut belum ditrim. coba cek data agan: pada file controller.php $nama = $_POST['username']; (tidak ada validasi untuk menghilangkan spasi dengan fungsi trim()) pada file model.php $nama = $this->escape($nama); (hanya escape saja tidak ada trim()); sehingga variable nama yang belum ditrim tersebut jelas akan masuk ke database. jadinya tidak muncul peringatan. sedangkan pada fungsi if(!empty(trim($nama)) itu hanya syarat saja tapi trim($nama)-nya tidak agan oper. coba kasi ini gan dibawah $nama = $_POST['username'] : $nama = trim(preg_replace('/\s+/',' ', $nama));

avatar onirusama

@onirusama

265 Kontribusi 183 Poin

Dipost: 6 tahun yang lalu Update 6 tahun yang lalu


terima kasih kodenya sangat sangat berguna dan berhasil :) karakter \s (white space) karakter + (ada atau lebih) ?? boleh di jelasin yg preg_replace dlm kurung takut saya salah meng artikan .terima kasih