{"id":202,"date":"2012-02-01T07:53:31","date_gmt":"2012-02-01T07:53:31","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=202"},"modified":"2025-09-21T14:01:00","modified_gmt":"2025-09-21T13:01:00","slug":"convert-strings","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/strings\/convert-strings","title":{"rendered":"Convert Strings"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Converting to HTML, from form POST, etc<\/h4>\n\n\n\n<p>See <a href=\"https:\/\/ibex.tech\/cloud\/php\/strings\/sanitising-strings\">here<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"> Converting String To Variable<\/h4>\n\n\n\n<p>\nYou don&#8217;t need to, but if you want to force to a particular type you can:\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $DeviceId = intval($DeviceId);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Converting Variables To String<\/h4>\n\n\n\n<p>\nNo need, PHP automatically converts a varaible to the type needed for the context being used\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $number = 12345 &amp; 67890;   \/\/$number is used as a numeric varaible\n  echo substr ($number, 3, 1);   \/\/$number is used as a string<\/code><\/pre>\n\n\n\n<p>If you want to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyString = (string)123;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Convert copy and pasted text<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/Convert copy and pasted email addresses (seperate email addreses by comma or newline or both)\n  $emails = preg_split(\"\/&#91;\\r\\n,]+\/\", $email_addresses, -1, PREG_SPLIT_NO_EMPTY);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Remove special characters from a string<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/Remove all special characters\n  $MyString = str_replace(' ', '-', MyString);                  \/\/Replaces all spaces with hyphens.\n  $MyString = preg_replace('\/&#91;^A-Za-z0-9\\-]\/', '', $MyString);  \/\/Remove all special characters\n  \n  \/\/This string: 'a|\"bc!@\u00a3de^&amp;$f g'\n  \/\/Will become: 'abcdef-g'<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Converting to HTML, from form POST, etc See here Converting String To Variable You don&#8217;t need to, but if you want to force to a particular type you can: Converting Variables To String No need, PHP automatically converts a varaible to the type needed for the context being used If you want to: Convert copy [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/202","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=202"}],"version-history":[{"count":10,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":5112,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/202\/revisions\/5112"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}