{"id":15407,"date":"2011-08-18T14:15:26","date_gmt":"2011-08-18T12:15:26","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=15407"},"modified":"2011-08-18T14:15:26","modified_gmt":"2011-08-18T12:15:26","slug":"php-regular-expression-to-match-englishlatin-characters-only","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2011\/08\/18\/php-regular-expression-to-match-englishlatin-characters-only\/","title":{"rendered":"PHP regular expression to match English\/Latin characters only"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>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 to force users to input their registration details in English only. \u00a0Using Latin characters, numbers, and punctuation.<\/p>\n<p>I&#8217;ve refreshed my knowledge of <a href=\"http:\/\/www.regular-expressions.info\/unicode.html\">Unicode<\/a> and <a href=\"http:\/\/www.php.net\/manual\/en\/book.pcre.php\">PCRE<\/a>. \u00a0And then I came up with the following method which seems to do the job just fine.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/**\r\n * Check that given string only uses Latin characters, digits, and punctuation\r\n *\r\n * @param string $string String to validate\r\n * @return boolean True if Latin only, false otherwise\r\n *\/\r\npublic function validateLatin($string) {\r\n    $result = false;\r\n\r\n    if (preg_match(&quot;\/^&#x5B;\\w\\d\\s.,-]*$\/&quot;, $string)) {\r\n        $result = true;\r\n    }\r\n\r\n    return $result;\r\n}\r\n<\/pre>\n<p>In other words, just a standard regular expression with no Unicode trickery. \u00a0The &#8216;\/u&#8217; modifier would cause this to totally malfunction and match everything. \u00a0Good to know.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>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 to force users to input &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2011\/08\/18\/php-regular-expression-to-match-englishlatin-characters-only\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PHP regular expression to match English\/Latin characters only<\/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,23],"keyring_services":[],"class_list":["post-15407","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","tag-php","tag-unicode"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":14892,"url":"https:\/\/mamchenkov.net\/wordpress\/2011\/05\/17\/partial-string-replacement-with-fixed-length-in-php\/","url_meta":{"origin":15407,"position":0},"title":"Partial string replacement with fixed length in PHP","author":"Leonid Mamchenkov","date":"May 17, 2011","format":false,"excerpt":"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\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":15407,"position":1},"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":26269,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/07\/21\/the-regex-that-killed-stackoverflow\/","url_meta":{"origin":15407,"position":2},"title":"The RegEx that killed StackOverflow","author":"Leonid Mamchenkov","date":"July 21, 2016","format":false,"excerpt":"Here's an outage postmortem from the recent StackOverflow downtime. \u00a0It just shows you how easy it is to break things, even they were built by some of the smartest people around. \u00a0Programming is touch and there is no way around it. Technical Details The regular expression was: ^[\\s\\u200c]+|[\\s\\u200c]+$ Which is\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":15407,"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":26949,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/11\/24\/quick-way-to-create-a-php-stdclass\/","url_meta":{"origin":15407,"position":4},"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":26687,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/09\/14\/mysql-php-and-integrity-constraint-violation-1062-duplicate-entry\/","url_meta":{"origin":15407,"position":5},"title":"MySQL, PHP and &#8220;Integrity constraint violation: 1062 Duplicate entry&#8221;","author":"Leonid Mamchenkov","date":"September 14, 2016","format":false,"excerpt":"Anna Filina blogs about an interesting problem she encountered with when working on a PHP and MySQL project: MySQL was complaining about \u201cIntegrity constraint violation: 1062 Duplicate entry\u201d. I had all the necessary safeguards in my code to prevent duplicates in tha column. I gave up on logic and simply\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\/15407","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=15407"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/15407\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=15407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=15407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=15407"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=15407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}