{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">JAVASCRIPT CODE<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">preg_replace(&quot;\/[^A-Za-z0-9 ]\/&quot;, &#039;&#039;, $string);<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\"> preg_replace(&quot;\/[^[:alnum:][:space:]]\/u&quot;, &#039;&#039;, $string);<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$str = preg_replace(&#039;\/[^a-z\\d ]\/i&#039;, &#039;&#039;, $str);<\/code><\/pre> <\/div>\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&#8217;t 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&#8217;s 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">  \\W|_<\/code><\/pre> <\/div>\n<p>and used as you need it (with a forward \/ slash delimiter).<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\"> preg_replace(&quot;\/\\W|_\/&quot;, &#039;&#039;, $string);<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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\/\" target=\"_blank\" rel=\"noopener\">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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">function onlyAlphanumericAndSpaces($text) <br\/>{<br\/>  # allow only alphanumeric<br\/>  return ereg_replace(&quot;[^A-Za-z0-9 ]&quot;, &quot;&quot;, $text );<br\/>}<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">\/**<br\/>* Remove all characters except numbers.<br\/>*<br\/>* @param string $string<br\/>* @return string<br\/>*\/<br\/>function stripNonNumeric( $string ) <br\/>{<br\/>    return preg_replace( &quot;\/[^0-9]\/&quot;, &quot;&quot;, $string );<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php $string = &quot;Here! is some text, and numbers 12345, and symbols !\u00a3$%^&amp;&quot;;<br\/>$new_string = preg_replace(&quot;\/[^a-zA-Z0-9\\s]\/&quot;, &quot;&quot;, $string);<br\/>echo $new_string ?&gt;<\/code><\/pre> <\/div>\n<p>&nbsp;<\/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}]}}