{"id":3435,"date":"2017-04-01T18:47:20","date_gmt":"2017-04-01T13:17:20","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3435"},"modified":"2018-10-23T19:58:36","modified_gmt":"2018-10-23T14:28:36","slug":"remove-non-alphanumeric-characters","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/remove-non-alphanumeric-characters\/","title":{"rendered":"[ Solved -7 Answers] &#8211; How to remove non-alphanumeric characters from a string"},"content":{"rendered":"<h2 id=\"problem\"><span style=\"color: #ff0000;\"><label class=\"label label-warning\">PROBLEM:<\/label><\/span><\/h2>\n<ul>\n<li>How to remove non-alphanumeric characters from a <a href=\"https:\/\/www.wikitechy.com\/technology\/java-programming-compare-two-strings-represented-linked-lists\/\">string<\/a><\/li>\n<\/ul>\n<h2 id=\"solution-1\"><span style=\"color: #008000;\">SOLUTION 1:<\/span><\/h2>\n<ul>\n<li>\u00a0To remove non-alphanumeric <a href=\"https:\/\/www.wikitechy.com\/technology\/get-first-n-characters-string\/\">characters<\/a> from a string,first you need to basically defined it as a regex.<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dpreg_replace(%22%2F%5B%5EA-Za-z0-9%20%5D%2F%22%2C%20\u201d%2C%20%24string)%3B%0A\u201d message=\u201dJAVASCRIPT CODE\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h2 id=\"solution-2\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 2:<\/label><\/span><\/h2>\n<ul>\n<li>For <a href=\"https:\/\/www.wikitechy.com\/technology\/executing-java-code-comments-certain-unicode-characters-allowed\/\">unicode<\/a> characters, we have to use the below <span style=\"color: #ff6600;\">example<\/span>:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%20preg_replace(%22%2F%5B%5E%5B%3Aalnum%3A%5D%5B%3Aspace%3A%5D%5D%2Fu%22%2C%20\u201d%2C%20%24string)%3B%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h2 id=\"solution-3\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 3:<\/label><\/span><\/h2>\n<ul>\n<li><b>We can also use the regular<a href=\"https:\/\/www.wikitechy.com\/technology\/c-programming-program-evaluate-simple-expressions\/\"> expression<\/a>.<\/b><\/li>\n<\/ul>\n<p><span style=\"color: #ff00ff;\">\u00a0 \u00a0Here is an example:<\/span><\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24str%20%3D%20preg_replace(\u2018%2F%5B%5Ea-z%5Cd%20%5D%2Fi\u2019%2C%20\u201d%2C%20%24str)%3B%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"we-need-to-note-of-the-following\"><span style=\"color: #ff9900;\">we need to\u00a0 note of the following<\/span><\/h3>\n<ul>\n<li>The i stands for case insensitive.<\/li>\n<li>^ means, does not start with.<\/li>\n<li>\\d matches any digit.<\/li>\n<li>a-z matches all characters between a and z. Because of the i parameter you don\u2019t have to specify a-z and A-Z.<\/li>\n<li>After \\d there is a space, so spaces are allowed in this <a href=\"https:\/\/www.wikitechy.com\/technology\/linux-regular-expressions\/\">regex<\/a><\/li>\n<\/ul>\n<h2 id=\"solution-4\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 4:<\/label><\/span><\/h2>\n<ul>\n<li>here\u2019s a really simple regex for <a href=\"https:\/\/www.wikitechy.com\/technology\/java-algorithm-remove-duplicates-unsorted-linked-list\/\">remove <\/a>non-alpha numeric characters:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%20%20%5CW%7C_%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>and used as you need it (with a forward \/ slash delimiter).<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%20preg_replace(%22%2F%5CW%7C_%2F%22%2C%20\u201d%2C%20%24string)%3B%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>Test it here with this great tool that explains what the regex is doing:<\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.regexr.com\/\">http:\/\/www.regexr.com\/<\/a><\/p>\n<h2 id=\"solution-5\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 5:<\/label><\/span><\/h2>\n<ul>\n<li>A\u00a0<a href=\"https:\/\/www.wikitechy.com\/technology\/find-php-ini-file-used-command-line\/\">PHP<\/a> function that strips a string of all characters other than alphanumeric characters:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dfunction%20onlyAlphanumericAndSpaces(%24text)%20%0A%7B%0A%20%20%23%20allow%20only%20alphanumeric%0A%20%20return%20ereg_replace(%22%5B%5EA-Za-z0-9%20%5D%22%2C%20%22%22%2C%20%24text%20)%3B%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h2 id=\"solution-6\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 6:<\/label><\/span><\/h2>\n<ul>\n<li><span style=\"color: #ff6600;\"><b>We can also use the below code to Remove All <a href=\"https:\/\/www.wikitechy.com\/technology\/generate-random-alpha-numeric-string\/\">Non-Numeric<\/a> Characters<\/b><\/span><\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%2F**%0A*%20Remove%20all%20characters%20except%20numbers.%0A*%0A*%20%40param%20string%20%24string%0A*%20%40return%20string%0A*%2F%0Afunction%20stripNonNumeric(%20%24string%20)%20%0A%7B%0A%20%20%20%20return%20preg_replace(%20%22%2F%5B%5E0-9%5D%2F%22%2C%20%22%22%2C%20%24string%20)%3B%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h2 id=\"solution-7\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 7:<\/label><\/span><\/h2>\n<ul>\n<li>This code will delete any non-alphanumeric characters specified between the brackets, and then <a href=\"https:\/\/www.wikitechy.com\/technology\/using-linux-echo-command-output-text-screen\/\">output<\/a>\/return the clean version.<\/li>\n<\/ul>\n<h3 id=\"the-code\"><span style=\"color: #993300;\"><b>The code:<\/b><\/span><\/h3>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%20%24string%20%3D%20%22Here!%20is%20some%20text%2C%20and%20numbers%2012345%2C%20and%20symbols%20!%C2%A3%24%25%5E%26%22%3B%0A%24new_string%20%3D%20preg_replace(%22%2F%5B%5Ea-zA-Z0-9%5Cs%5D%2F%22%2C%20%22%22%2C%20%24string)%3B%0Aecho%20%24new_string%20%3F%3E%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to remove non-alphanumeric characters from a string &#8211; To remove non-alphanumeric characters from a string, you need to basically defined it as a regex.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[85572,2134,6348,1128,6352,6353,85566,85567,85565,85569,85568,6351,6349,85570,6350,6347,85571,6346],"class_list":["post-3435","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-groovy-remove-character-from-string","tag-how-do-i-check-if-a-string-contains-a-specific-word-in-php","tag-how-do-i-remove-all-non-alphanumeric-characters-from-a-string-except-dash","tag-how-to-check-whether-a-string-contains-a-substring-in-javascript","tag-how-to-remove-any-non-alphanumeric-characters","tag-how-to-remove-non-alphanumeric-characters-c","tag-how-to-remove-special-characters-and-numbers-from-a-string-in-java","tag-java-remove-all-non-numeric-characters-from-string","tag-java-remove-non-alphabetic-characters-from-string","tag-javascript-remove-non-alphanumeric-and-spaces","tag-regex-replace-all-non-alphanumeric-characters-javascript","tag-remove-all-non-alphanumeric-characters-using-preg_replace","tag-remove-new-lines-from-string","tag-remove-special-characters-from-string-java","tag-remove-the-last-character-from-string","tag-removing-non-alphanumeric-characters-from-a-string","tag-scala-remove-character-from-string","tag-using-a-regular-expression-to-validate-an-email-address"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3435"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3435\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}