{"id":22,"date":"2013-12-25T12:56:16","date_gmt":"2013-12-25T10:56:16","guid":{"rendered":"http:\/\/en.nisi.ro\/blog\/?p=22"},"modified":"2014-04-23T08:32:19","modified_gmt":"2014-04-23T06:32:19","slug":"php-trim-spaces-middle-string","status":"publish","type":"post","link":"https:\/\/en.nisi.ro\/blog\/development\/php\/php-trim-spaces-middle-string\/","title":{"rendered":"PHP: trim spaces in the middle of a string"},"content":{"rendered":"<p>When you let users input text in a textfield or input box it is always a good idea to use trim function to remove spaces form the beginning or the end of the string.<\/p>\n<p>Today I needed a function to trim spaces from the middle of a string and replace with a single space.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\/*\r\n    this will return the string where groups of consecutive spaces\r\n    are collapsed in a single space\r\n\r\n    eg. $str = &quot;This is    my test    string&quot;;\r\n    function will output &quot;This is my test string&quot;\r\n *\/\r\nfunction centerTrim($str){\r\n    return preg_replace(&quot;\/\\s+\/&quot;, &quot; &quot;, $str)\r\n}<\/pre>\n<p><!--more-->if you need to remove all spaces you can use this function:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\/*\r\n    eg. $str = &quot;This is    my test    string&quot;;\r\n    function will output &quot;Thisismyteststring&quot;\r\n *\/\r\nfunction centerTrim2($str){\r\n    return preg_replace(&quot;\/\\s+\/&quot;, &quot;&quot;, $str)\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When you let users input text in a textfield or input box it is always a good idea to use trim function to remove spaces form the beginning or the end of the string. Today I needed a function to trim spaces from the middle of a string and replace with a single space. \/* [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[34,31,33,32,30],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-php","tag-centertrim","tag-php-trim","tag-preg_replace","tag-trim-middle","tag-trim-spaces"],"_links":{"self":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":2,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}