{"id":14892,"date":"2011-05-17T14:19:25","date_gmt":"2011-05-17T12:19:25","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=14892"},"modified":"2011-05-17T16:49:54","modified_gmt":"2011-05-17T14:49:54","slug":"partial-string-replacement-with-fixed-length-in-php","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2011\/05\/17\/partial-string-replacement-with-fixed-length-in-php\/","title":{"rendered":"Partial string replacement with fixed length in PHP"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>Today I came across one of those problems that look simple and straight forward at first, but then somehow consume a good part of an hour to solve. \u00a0I wanted to replace a part of the string (in the middle), with given character, without changing the length of the string itself. \u00a0Once scenario would be a credit card number, where I would want to replace all digits with an &#8216;x&#8217;, leaving only the first digit and the last four as they were. \u00a0I tried a number of approaches, including <em>sscanf()<\/em> and <em>array_splice()<\/em> ones. \u00a0My final version is below. \u00a0It might be somewhat longer than I expected, but I like the simplicity of it.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/**\r\n * Replace middle of the string with given char, keeping length\r\n *\r\n * &lt;code&gt;\r\n * print padString('1234567890123456', 1, 4, 'x');\r\n * &lt;\/code&gt;\r\n *\r\n * @param string $string String to process\r\n * @param integer $start Characters to skip from start\r\n * @param integer $end Characters to leave at the end\r\n * @param string $char Character to replace with\r\n * @return string\r\n *\/\r\nfunction padString($string, $start, $end, $char) {\r\n    $result = '';\r\n\r\n    $length = strlen($string) - $start - $end;\r\n    if ($length &lt;= 0) {\r\n        $result = $string;\r\n    }\r\n    else {\r\n        $replacement = sprintf(&quot;%'{$char}&quot; . $length . &quot;s&quot;, $char);\r\n        $result = substr_replace($string, $replacement, $start, $length);\r\n    }\r\n\r\n    return $result;\r\n}\r\n?&gt;\r\n<\/pre>\n<p>P.S.: Chris posted an <a href=\"http:\/\/blog.ergatides.com\/web-design\/masking-fixed-length-strings-in-php\/\">alternative solution<\/a>. Please have a look.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>Today I came across one of those problems that look simple and straight forward at first, but then somehow consume a good part of an hour to solve. \u00a0I wanted to replace a part of the string (in the middle), with given character, without changing the length of the string itself. \u00a0Once scenario would be &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2011\/05\/17\/partial-string-replacement-with-fixed-length-in-php\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Partial string replacement with fixed length in PHP<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,62],"tags":[38],"keyring_services":[],"class_list":["post-14892","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","tag-php"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":22296,"url":"https:\/\/mamchenkov.net\/wordpress\/2014\/07\/29\/the-goal-challenge\/","url_meta":{"origin":14892,"position":0},"title":"The goal() challenge","author":"Leonid Mamchenkov","date":"July 29, 2014","format":false,"excerpt":"I came across the goal() challenge this morning. g()('al') is a challenge whereby you need to write in as many languages as possible code which enables the code g()('al') to return the string \"goal\", the code g()()('al') to return the string \"gooal\", the code g()()()('al') return the string \"goooal\", etc.\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26949,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/11\/24\/quick-way-to-create-a-php-stdclass\/","url_meta":{"origin":14892,"position":1},"title":"Quick way to create a PHP stdClass","author":"Leonid Mamchenkov","date":"November 24, 2016","format":false,"excerpt":"Simon Holywell shows how to quickly create the stdClass in PHP and populate it with properties and values, by casting an array to an object: [code lang=\"php\"] $x = (object) [ 'a' => 'test', 'b' => 'test2', 'c' => 'test3' ]; var_dump($x); \/* object(stdClass)#1 (3) { [\"a\"]=> string(4) \"test\" [\"b\"]=>\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27350,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/02\/13\/morphos-morphological-solution-in-php-for-english-and-russian\/","url_meta":{"origin":14892,"position":2},"title":"Morphos &#8211; morphological solution in PHP for English and Russian","author":"Leonid Mamchenkov","date":"February 13, 2017","format":false,"excerpt":"If you ever had to deal with morphology in English, you probably found one or two libraries to help you out. \u00a0But if you had to do that for Russian, than I'm sure you are missing a few hairs, and the ones that you still have are grayer than they\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":29105,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/12\/17\/the-best-way-to-get-the-full-php-version-string\/","url_meta":{"origin":14892,"position":3},"title":"The best way to get the full PHP version string","author":"Leonid Mamchenkov","date":"December 17, 2018","format":false,"excerpt":"Jeff Geerling shares the best way to get the full PHP version string.\u00a0 I'd think that \"php --version\" externally or \"echo PHP_VERSION\" internally would do the job.\u00a0 However, that's not exactly right, as there are a number of inconsistencies on different platforms.\u00a0 The best option seems to be the combination\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":15407,"url":"https:\/\/mamchenkov.net\/wordpress\/2011\/08\/18\/php-regular-expression-to-match-englishlatin-characters-only\/","url_meta":{"origin":14892,"position":4},"title":"PHP regular expression to match English\/Latin characters only","author":"Leonid Mamchenkov","date":"August 18, 2011","format":false,"excerpt":"Today at work I came across a task which turned out to be much easier and simpler than I originally thought it would. \u00a0We have have a site with some user registration forms. \u00a0The site is translated into a number of languages, but due to the regulatory procedures, we have\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26031,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/04\/18\/single-sign-on-between-sugarcrm-and-request-tracker\/","url_meta":{"origin":14892,"position":5},"title":"Single Sign-On Between SugarCRM and Request Tracker","author":"Leonid Mamchenkov","date":"April 18, 2016","format":false,"excerpt":"As mentioned\u00a0before,\u00a0over the last few month I've been involved in quite a few integration projects, using mostly SugarCRM and Request Tracker. \u00a0One of the interesting challenges was the Single Sign-On (SSO) between the two. The interesting bit comes from these facts: Different technologies: SugarCRM is written in PHP, while Request\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/14892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=14892"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/14892\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=14892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=14892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=14892"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=14892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}