{"id":3293,"date":"2020-09-18T12:40:05","date_gmt":"2020-09-18T11:40:05","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3293"},"modified":"2025-04-07T10:59:32","modified_gmt":"2025-04-07T09:59:32","slug":"converting-form-text-fields","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/strings\/converting-form-text-fields","title":{"rendered":"Converting Form Text Fields"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">filter_var() function<\/h4>\n\n\n\n<p>Returns the input string filtered into the required typ, or FALSE if it was unable to perform the sanitization (e.g. due to illegal characters etc)<\/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&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.php.net\/manual\/en\/filter.filters.sanitize.php\" target=\"_blank\">here<\/a>&nbsp;for all the available filter options.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Example \u2013 INT<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  if ( ($MyVariable = filter_var($SourceValue, FILTER_VALIDATE_INT, &#91;\"options\" =&gt; &#91;\"min_range\" =&gt; 18, \"max_range\" =&gt; 124]])) !== False )\n  {\n\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Sanitising for HTML from a form POST<\/h4>\n\n\n\n<p>If server magic quotes is turned on then it will add \/ before a single and double quotes in the text, so strip them out first:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyTextField = trim($_POST&#91;'MyTextField']);\n  $MyTextField = stripslashes($MyTextField);\n  $MyTextField = htmlspecialchars($MyTextField, ENT_QUOTES);<\/code><\/pre>\n\n\n\n<p>\\n new line characters will still be stored as \\n (htmlspecialchars() doesn\u2019t convert them to &lt;br>). You can convert them now using nl2br() or when you display the text again (usually better to do when displaying as HTML, not when storing to db).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyTextField = nl2br($MyTextField);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">When outputting multiline text &#8211; Convert \\n character to &lt;br&gt; when displaying text within page html output<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyTextField = Results&#91;'MyTextField'];\n  $MyTextField = nl2br($MyTextField);    \/\/Convert '\\n' to &lt;br&gt;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Removing new lines in a text box<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$MyText = str_replace(\"\\r\\n\",\" \", $MyText);\n$MyText = str_replace(\"\\n\",\" \", $MyText);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Removing other characters<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyTextField = str_replace(\"\\t\",\" \", $MyTextField);\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Other slashes functions that are available<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>stripcslashes()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>filter_var() function Returns the input string filtered into the required typ, or FALSE if it was unable to perform the sanitization (e.g. due to illegal characters etc) See&nbsp;here&nbsp;for all the available filter options. Example \u2013 INT Sanitising for HTML from a form POST If server magic quotes is turned on then it will add \/ [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,19],"tags":[],"class_list":["post-3293","post","type-post","status-publish","format-standard","hentry","category-forms","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3293","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=3293"}],"version-history":[{"count":15,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3293\/revisions"}],"predecessor-version":[{"id":4818,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3293\/revisions\/4818"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}