{"id":195,"date":"2012-01-31T20:48:37","date_gmt":"2012-01-31T20:48:37","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=195"},"modified":"2025-09-25T13:02:10","modified_gmt":"2025-09-25T12:02:10","slug":"sanitising-strings","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/strings\/sanitising-strings","title":{"rendered":"Sanitising &#038; encoding strings"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Sanitising for HTML<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$MyString = htmlspecialchars($MyString, ENT_QUOTES);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ '&amp;' (ampersand) becomes '&amp;'\n\/\/ '\"' (double quote) becomes '\"'\n\/\/ \"'\" (single quote) becomes '''\n\/\/ '&lt;' (less than) becomes '&lt;'\n\/\/ '&gt;' (greater than) becomes '&gt;' <\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Convert special HTML entities back to characters<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$MyString = htmlspecialchars_decode($MyString);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">URL Encode and Decode<\/h4>\n\n\n\n<p>Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.   A space is encoded to %20 in URLs, and to + in forms submitted data (content type application\/x-www-form-urlencoded).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $UrlString = urlencode($OriginalString);\n  $OriginalString = urldecode($UrlString);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Sanitising for HTML from a form POST<\/h4>\n\n\n\n<p>See <a href=\"\/cloud\/php\/strings\/converting-form-text-fields\">page here<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">filter_var() function<\/h4>\n\n\n\n<p>Returns the input string filtered, or FALSE if it was unable to perform the sanitization (e.g. due to an illegal character)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  if ( ($MyVariable = filter_var($EnteredEmail, FILTER_SANITIZE_EMAIL)) !== False )<\/code><\/pre>\n\n\n\n<p>See <a rel=\"noreferrer noopener\" href=\"https:\/\/www.php.net\/manual\/en\/filter.filters.sanitize.php\" target=\"_blank\">here<\/a> for all the available filter options<\/p>\n\n\n\n<p>See <a href=\"\/php\/strings\/converting-form-text-fields\">here<\/a> for examples of using filter_var().<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Example<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyString = \"This is my sample text, with special chars. #%!\\\"'^-_\u00a3&amp;\";\n  echo \"Start string: $MyString&lt;br&gt;\";\n  \n  $MyString = urlencode($MyString);\n  echo \"urlencode(): $MyString&lt;br&gt;\";\n  \n  $MyString = urldecode($MyString);\n  echo \"urldecode(): $MyString&lt;br&gt;\";\n\n  \/\/Produces:\n  \/\/  Start string: This is my sample text, with special chars. #%!\"'^-_\u00a3&amp;\n  \/\/  urlencode(): This+is+my+sample+text%2C+with+special+chars.+%23%25%21%22%27%5E-_%C2%A3%26\n  \/\/  urldecode(): This is my sample text, with special chars. #%!\"'^-_\u00a3&amp;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">If wanting to pass a file url in an argument you can do this<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/The HTML Link with the URL argument  \n$Url .= '&lt;a href=\"\/my_file?iurl=' . urlencode($MyStringContainingAUrl) . '\/\" &gt;';    \/\/We add a trailing '\/' otherwise a file extension period '.' in $MyStringContainingAUrl buggers up the argument being seen as one and not a file link to the browser\n  \n\/\/The page the argument was passed to\n$MyStringContainingAUrl .= '&lt;img src=\"' . rtrim(urldecode($_REQUEST&#91;'iurl']), '\/') . '\" &gt;';      \/\/Remove the trailing '\/' that was added to avoid the period breaking the url argument<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">PHP adds back slash before forward slash<\/h4>\n\n\n\n<p>(e.g. \/ becomes \\\/ )<\/p>\n\n\n\n<p>It&#8217;s a JSON issue.  JSON escapes all special characters by default. When decoded, you will get original value back without the backslash.  If its causing issues you need to resolve see stripslashes tip about needing to be at final echo  <a href=\"https:\/\/ibex.tech\/cloud\/php\/strings\/special-characters-in-strings\">here<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">General PHP only use<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>function SanitizeString($var)\n{\n\t$var = strip_tags($var);\n\t$var = htmlentities($var);\n\treturn stripslashes($var);\n}\n\/\/OR JUST USE THIS\n$my_string = stripslashes(htmlentities(strip_tags($my_string)));<\/code><\/pre>\n\n\n\n<p><span style=\"color: #e6891b; font-size: 16px;\">htmlentities<\/span><\/p>\n\n\n\n<p>htmlentities() converts things like &lt; &gt; &#8221; \\ etc into HTML strings like &amp;lt; so they become harmless.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $CameFromPage = htmlentities($_SERVER&#91;'HTTP_REFERER']);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Stopping New Lines In A Text Box Being Converted To &lt;br \/&gt;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$my_text = mysqli_real_escape_string($dblink, str_replace(\"\\r\\n\",\" \",$_POST&#91;'myform_text_field'])); <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sanitising for HTML Convert special HTML entities back to characters URL Encode and Decode Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. A space is encoded to %20 in URLs, and to + [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,19],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-security","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":33,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":5123,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/195\/revisions\/5123"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}