PHP: automatically replace alphanumeric characters in a string

Quick question:
How do I program a PHP script to replace all non-alphanumeric
charters within a string, with underscores?

.oO(AngryCloud)
>Yes, this does help, although it is only alphabetic.
>
>Will changing the line to this make it alphanumeric?:
>
>$new_string = preg_replace("/[^a-zA-Z0-9]/", "_",
$string);
Did you try it? ;-)
You could also use this shorter pattern:
/[^a-z\d]/i
Should be the same (\d matches decimals and the /i modifier
makes the
entire thing case-insensitive).
Micha

Similar Messages

  • Need help in replacing special characters in a string

    Hi,
    please let me know the best way to replace all the special characters in a string with space.
    other than alphabets and numbers
    with regards.
    sumanth.

    please let me know the best way to replace all the special characters in a string with space.
    other than alphabets and numbers
    >
    Sumanth Nag Kristam wrote:
    > actually i need to replace hexa decimal char 0X1A in a string.... that is 'substitue' as per the chart
    > any pointers....
    >
    > chk the link for the ASCII codes
    > http://www.techonthenet.com/ascii/chart.php
    But in Hexa decimal value there is no special characters?

  • Replace multiple characters in a string

    Hi, I have some string fields that contain special characters such as ô and û and I want to replace them with ō and ū, and their upper case equivalents as well. How do I write a single formula that would find any of these characters in a string and replace them?
    Thanks,
    Will

    replace(replace(replace(replace(x,'ô','ō'),'û','ū'),'Ô','Ö'),'Û','Ü');
    where x is the string field.  I suggest using the unicodes rather than the actual character.  I do not think that I have the correct uppercase characters.  Please ensure that they are correct in your formula.

  • Regular expressions-how to replace [ and ] characters from a string

    Hi,
    my input String is "sdf938 [98033]". Now from this given string, i would like to replace the characters occurring within square brackets to empty string, including the square brackets too.
    my output String needs to be "sdf938" in this case.. How should I do it using regular expressions? I tried several possible combinations but didn't get the expected results.

    "\\s*\\[[^\\]]+\\]"

  • Replace Special Characters in a String

    Let's say someone copies the folowing list and pastes it into a Javascript prompt box:
    302304
    305678
    245675
    How do I manipulate the string so it reads:
    302304 305678 245675
    In other words, how do I replace the manual line breaks in a string with spaces?
    Thanks!

    \r and \n are both \s characters.
    This is functionally equivalent to what you have...
    stringName = stringName.replace(/\s/g,' ');
    I highly recommend browsing this website (lots of useful info there):
    http://www.regular-expressions.info/
    Harbs

  • Replacing Unicode characters in a String

    I have a text as a String and in this text which is foreign language appears sometimes characters in Unicode format as ö or Ö
    The question is, how can I convert this String so these Unicode characters would become readable characters.
    Thank you for any lead...

    :-) Interesting - twice in 10 minutes I'm recommending the use of Elliott Hughes' class. See http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html .
    You just have to write a regular expression to match the terms ( "&#(\\\d+)"), extract the number as a string (group(1)) then use the Integer.parseInt(the numeric string) and cast to a char.
    Edited by: sabre150 on Feb 9, 2012 9:53 AM
    This is an example I wrote a couple of years ago (and published on the old Sun site forums) that does pretty much exactly what you need
    import e.util.Rewriter;
    public class Sabre20090919
        public static void main(String[] args) throws Exception
            String title = "& #26412;& #26399;& #28136;& #21033;\n" +
                    "& #22522;& #26412;& #27599;& #32929;& #30408;& #39192;\n" +
                    "& #32380;& #32396;& #29151;& #26989;& #21934;& #20301;& #28136;& #21033;\n" +
                    "& #26371;& #35336;& #21407;& #21063;& #35722;& #21205;& #32047;& #31309;& #24433;& #38911;& #25976;\n" +
                    "& #26222;& #36890;& #32929;& #27599;& #32929;& #30408;& #39192;\n" +
                    "& #31232;& #37323;& #27599;& #32929;& #30408;& #39192;\n";
            Rewriter rewriter = new Rewriter("&\\s*#(\\d+);")
                @Override
                public String replacement()
                    return Character.toString((char) Integer.parseInt(group(1)));
            title = rewriter.rewrite(title);
            System.out.println(title);
            System.out.print("Unicode :\"");
            for (char ch : title.toCharArray())
                System.out.printf("\\u%04x", (int) ch);
            System.out.println("\"");
            byte[] asBytes = title.getBytes("big5");
            for (byte b : asBytes)
                System.out.printf("%02x ", (int) (b & 0xff));
            System.out.println();
    }The rest is just a test harness.
    Edited by: sabre150 on Feb 9, 2012 10:02 AM
    Removed redundant code

  • How to replace escape characters in a string ?

    Hi All,
    In my application I came across a problem where I want to replace a substring (contains escape characters also) with another string. The below shown code will replicate my problem :
    public class StringSearchAndReplace {
      public static void main(String args[])   {
        String stmt = " \\pntext\\bullet\\tab The question as to ";
        String newStmt = stmt.replaceAll("\\bullet\\tab",  "B");
        System.out.println("BEFORE: " + stmt + "\n");
        System.out.println("AFTER: " + newStmt);
    }Here I want to replace "\\bullet\\tab" with "B". I am unable to move further. Please help/suggest me in this regard.
    Its urgent.
    Thanks in advance.

    Satyaprasad_Mylavarapu wrote:
    Hi All,
    In my application I came across a problem where I want to replace a substring (contains escape characters also) with another string. The below shown code will replicate my problem :
    public class StringSearchAndReplace {
    public static void main(String args[])   {
    String stmt = " \\pntext\\bullet\\tab The question as to ";
    String newStmt = stmt.replaceAll("\\bullet\\tab",  "B");
    System.out.println("BEFORE: " + stmt + "\n");
    System.out.println("AFTER: " + newStmt);
    }Here I want to replace "\\bullet\\tab" with "B". I am unable to move further. Please help/suggest me in this regard.
    Its urgent.
    Thanks in advance.If the String you're trying to replace contains a slash you need four slashes (double it for java, then again because String.replaceAll takes a regular expression)
    So I think what you're looking for is:
    String newStmt = stmt.replaceAll("\\\\bullet\\\\tab",  "B");I haven't tested that though.

  • Replace Special Characters in a string + XQUERY

    Hi All,
    I am using the following replace function to replace the special characters in my XQUERY.
    *replace($string1, '[&"-*;-`!|:,¿/{}@#$%^*~()_+-]', ' ')*
    Fortunately it is replacing all the special characters. But the only problem here is it is even replacing capitol letters in to spaces, which I don't want.
    Please help me out!!

    Hi
    <<< What did u change >?> I dont see any change with the Regex ?  >>
    if you observe in the modified function, he just removed (*) inside the replace funtion.
    Thanks
    Shankar AUNV

  • A clever way of replacing some characters in a string

    I have a string passed between methods in classes. Due to a face that I don't have all data ready when the string is constructed at the first place. I need to a few of sections in the string before I can use it. I am wondering any clever way to carry out this task.
    Here is a sample of such string:
    <web site name>/book.html?bookid=<book id data + series data>&language=<language data>
    where <web site name> <series data> from the first method and <book id data> and <language data> from the second method.

    One way would be to pass a String array that contains the sections that need to have other pieces inserted between them:
    [ "/book.html?bookid=" , "&language=" ]That's not a very flexible solution though, a better way would be to pass a Map<String, String> around, which holds the bookid, language, etc. Strings as keys, and lets the other methods map them to <book id data + series data>, <language data>, etc. values. Then when they're all populated, construct the final url from those values.
    That way if the params of the string change, it's easier to change the methods that populate the values.

  • Replace characters in a string

    Hi,
    I need to replace all occurrences of control characters except space,newline,tabs in a string . I can give a replace statement for each of these characters but I want to avoid this by making use of regular expressions. Can anyone help me in this regard.
    I tried using the following replace statements with regular expression, but i am not getting the required results:
    replace all occurrences of REGEX '[[:cntrl:]]' in lv_char with space replacement count lv_count_r.
    ---> this replaces even the spaces
    replace all occurrences of REGEX '[[:cntrl:]][^[:space:]]' in lv_char with space replacement count lv_count_r.
    --> this replaced even some alpha numeric characters
    Thanks and Regards,
    Shankar

    is there anyway to do this without using regular
    expressions.. regular expressions are the last
    solution for me..Remember that you can never really replace the characters of a String. Strings are immutable. Once created they cannot change.

  • Replace multiple characters to single character

    Hi friends,
    I would like to replace multiple characters in a string to a single character. The character may be a pipe (|) or a tilde (~).
    For example "|||asdf||123|xyz||" should be changed to "asdf|123|xyz".
    I use Oracle 11g2.
    Thanks in advance!

    Without regexp
    with testdata as (
        select '||asdf||||123|xyz|' str from dual union all
        select 'asdf|123|||||||xyz||||' from dual union all
        select '||||||asdf|||123||||||xyz||||||' from dual
    select
    trim (both '|' from
        replace (
            replace (
                replace (str,'|','|'||chr(0))
                ,chr(0)||'|'
            ,chr(0)
    ) str
    from testdata
    STR
    "asdf|123|xyz"
    "asdf|123|xyz"
    "asdf|123|xyz"
    with regexp
    with testdata as (
        select '||asdf||||123|xyz|' str from dual union all
        select 'asdf|123|||||||xyz||||' from dual union all
        select '||||||asdf|||123||||||xyz||||||' from dual
    select
    trim (both '|' from
        regexp_replace (
            str
            ,'\|+'
            ,'|'
    ) str
    from testdata
    STR
    "asdf|123|xyz"
    "asdf|123|xyz"
    "asdf|123|xyz"
    Message was edited by: chris227 regexp added

  • RegEx in TSQL - replace non-alphanumeric characters etc

    Hi guys, I have this function in VB that I used in Access to replace all non-alphanumeric characters, including spaces and anything in brackets.
    Public Function charactersonly(inputString As String) As String
    Dim RE As Object
    Set RE = CreateObject("vbscript.regexp")
    RE.Pattern = "\([^)]+\)|[^\w]|_"
    RE.Global = True
    charactersonly = RE.Replace(inputString, "")
    Set RE = Nothing
    End Function
    Now, I moved to SQL server and I'm writing scripts to do same thing.
    How can I use RegEx in TSQL?
    Only thing I will do is that function.

    As alternative
    declare @string varchar(200)
    set @string = 'gg$%^^&is%^& s2342jjk23&&({}e c76l232e+_+a#n/ c][#o''y#e'
    select cast(cast((select substring(@string,n,1)
    from numbers
    where n <= len(@string)
    and substring(@string,n,1) like '[0-9 ]' for xml path('')) as xml)as varchar(max))
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • How to replace special characters in string.

    Hello,
    I want to replace special characters such as , or ; with any other character or " ".I find out there is no such function is java for this.There is only replace() but it accepts only chars.If anybody know how to do this?.
    Thanks,

    Hello,
    I want to replace special characters such as , or ;
    with any other character or " ".I find out there is no
    such function is java for this.There is only replace()
    but it accepts only chars.If anybody know how to do
    this?.
    Thanks,Can't you just do the following?
    public class Test
         public static void main(String[] args)
         String testString = "Hi, there?";
         System.out.println(testString.replace(',',' '));
    }

  • Problem in replacing characters of a string ?

    Hello everybody,
    I want to replace a few characters with their corresponding unicode codepoint values.
    I have a userdefined method that gets the unicode codepoint value for a character.
    1. I want to know how to replace the characters and have the replaced string after the comparision is over in the for loop in my main.
    Currently , i am able to replace , but i am not able to have the replacements done in a single variable.
    The output of the code is
    e\u3006ame
    ena\u3005e
    But i want the output i require is,
    e\u3006a\u3005e
    Please offer some help in this regard
    import java.io.*;
    class Read1
         public static void main(String s[])
             String rp,snd;
             String tmp="ename";
             for(int i=0;i<tmp.length();i++)
                 snd=getCodepoint(tmp.charAt(i));
                 if(snd!=null)
                    rp=replace(tmp,String.valueOf(tmp.charAt(i)),"\\u"+snd);
                    System.out.println(rp);
    public static String replace(String source, String pattern, String replace)
         if (source!=null)
             final int len = pattern.length();
             StringBuffer sb = new StringBuffer();
             int found = -1;
             int start = 0;
             while( (found = source.indexOf(pattern, start) ) != -1)
                 sb.append(source.substring(start, found));
                 sb.append(replace);
                 start = found + len;
             sb.append(source.substring(start));
             return sb.toString();
         else return "";
    ...,Any help in this regard would be useful
    Thanks
    khurram

    This manual replacement thingy reminds me of quite an old technique, when
    64KB of memory was considered enough for 20 users (at the same time that is!)
    Suppose you have a buffer of, say, n characters. Starting at location i, a region
    of chars have to be swapped with bytes starting at location j >= i+l_i; the lengths
    of the two regions are l_i and l_j respectively.
    Suppose the following method is available:public void reverse(char[] buffer, int f, int l_f) {
       for (int t= f+l_f; --t > f; f++) {
          char tmp=buffer[f]; buffer[f]= buffer[t]; buffer[t]= tmp;
    }i.e. the above method reverses a region of characters, starting at position f
    with length l_f. Given this simple method, the original problem can be solved
    using the following simple sequence:reverse(buffer, i, j+l_j);
    reverse(buffer, i, l_j);
    reverse(buffer, i+l_j, j-i-l_i);
    reverse(buffer, j+l_j-l_i, l_i);Of course, when replacing characters we don't need the last reversal.
    kind regards,
    Jos (dinosaurus)

  • Produce random alphanumeric characters: dukes available

    Time for another duke giveaway
    On Saturday 6th March, approx 8.00PM (GMT) the following program will be run;-
    CodeMaker.javapublic class CodeMaker{
       public static void main(String []params){
          StringBuffer  sb = new StringBuffer();
          java.util.Random r = new java.util.Random();
          for(char c='A'; c<='Z'; c++) sb.append(c);
          for(char c='a'; c<='z'; c++) sb.append(c);
          for(int i=0; i<=10; i++) sb.append(i);
          for(int i=0; i<8; i++) System.out.print(sb.charAt(r.nextInt(sb.length()) ) );
    ALL ARE WELCOME TO GUESS THE OUTCOME OF THIS PROGRAM AND EARN FREE DUKES!
    - simply add to this thread with your guess of the 8 alphanumeric characters.
    - the more characters you guess right the more you earn.
    - only one guess permitted per contestant (schizoids such as the Scarlet Pimpernel excl.)
    - any other rules may or may not be made by me at any time (after all it is my game so ner-ner-nee-ner-ner)
    - that's all I can think of for the moment, good luck t'y'all.
    The points system awarded will be determined by the following program;-
    CodeScorer.javapublic class CodeScorer{
       public static void main(String []params){
          String luckyDukeWinner = params[0];
          String codeToCrack = params[1];
          String luckyDukeWinnerGuess = params[2];
          new CodeScorer(luckyDukeWinner, codeToCrack, luckyDukeWinnerGuess);
       private CodeScorer(String name, String code, String guess){
          int same = calcSame(code, guess, 0);
          int correct = calcCorrect(code, guess, 0);
          System.out.println("Number of letters correct = "+same);
          System.out.println("Number of letters exactly correct = "+correct);
          System.out.println("Dukes earned by "+name+": "+calcScore(same, correct));
       private int calcCorrect(String code, String guess, int tally){
          for (int i=0; i< code.length(); i++)
             if(guess.charAt(i) == code.charAt(i)) tally++;
          return tally;
       private int calcSame(String code, String guess, int tally){
          StringBuffer buf = new StringBuffer(code);
          for (int i=0; i< buf.length(); i++){
             for (int j=0; j< buf.length(); j++)
                if (guess.charAt(i) == buf.charAt(j)){
                   tally++;
                   buf.replace(j, j+1,"~");
          return tally;
       private int calcScore(int same, int correct){
          return (same>1 && correct>1)? (int)((correct*4)*(Math.pow(same,2))):(int)((correct*4)+(Math.pow(same,2)));
    }

    I thought this
    for(int i=0; i<=10; i++) sb.append(i);
    would do this
    for(int i=0; i<=10; i++) sb.append((char)i);
    I'm so humilitated....All punters may assume henceforth that line 7 char 22 is herewith removed (I'm humiliated too)

Maybe you are looking for