Number of shaped with preg_replace Regular Expression and PHP

Hello

I would like to add a 'dash' after every 3 digits in a given number (10 digits). For example, 9785678941 became 978-567-894-1. How could I achieve this with regular expression using PHP preg_replace?

Thank you.

The next solution is based on the example of "The use of backreferences followed literals digital" published on the php.net site.

In accordance with the $string, $pattern, $replacement nomenclature which is the php.net example use, here´s my modification:

<>

$string = '9785678941';

$pattern = ' / (\\d{3})(\\d{3})(\\d{3})(\\d{1)} /';

$replacement = ' ${1}-{2}-${3}-${4}';

echo preg_replace ($pattern, $replacement, $string);

?>

Tags: Dreamweaver

Similar Questions

  • Help with the Regular Expressions and regexp_replace

    Oh great guru Oracle can I can receive assistance

    I need to clean the phone numbers that have been entered in the table per_phones of Oracle e-Business. Some of the phone numbers have hyphens, some have spaces and some have tank. I just want to get out all the figures and then re - format the number.

    E.g.
    914-123-1234... out (914) 123-1234
    9141231234... new (914) 123-1234
    914 123 1234... (914) 123-1234
    MyPhone... just null
    (914)-123-1234... (914) 123-1234

    I really tried to understand the instructions of regular expressions, but for some reason, I can't understand it.

    For example:

    SQL> with sample_data as (
      2   select '914-123-1234' phone_number from dual union all
      3   select '9141231234'                from dual union all
      4   select '914 123 1234'              from dual union all
      5   select '(914)-123-1234'            from dual
      6  )
      7  select regexp_replace(
      8           regexp_replace(phone_number, '\D')
      9         , '(...)(...)(....)'
     10         , '(\1) \2-\3'
     11         ) as formatted_num
     12  from sample_data
     13  ;
    
    FORMATTED_NUM
    --------------------------------------------------------------------------------
    (914) 123-1234
    (914) 123-1234
    (914) 123-1234
    (914) 123-1234
     
    
  • When I try to download the disks of the instillation of a standard Adobe suite create 2 that I bought, the license number is associated with an old Photoshop and asks me to check this program and get on my new labtop. My new labtop has o

    When I try to download the disks of the instillation of a standard Adobe suite create 2 that I bought, the license number is associated with an old Photoshop and asks me to check this program and get on my new labtop. My new labtop has no former software adobe on it. Why can I simply use the install CD?

    If you have a cs2 license and you are trying to install cs2, error: "Activation Server unavailable". CS2, Acrobat 7, pass a hearing 3

  • Regular expressions and phone number

    Hello
    There is a column 'Phone_number Varchar2'
    Data containing:
    123 89556-6852

    -857 (123) - 965

    123-5846 5648

    I want to display that

    123895566852

    123857965

    12358465648

    Pls help by regular Expressions.

    Published by: Guillaume on February 17, 2010 15:59
    with t
    as
    (
    select '123-89556-6852' phone_no from dual union all
    select '(123)-857-965' from dual union all
    select '123-(5846)5648' from dual
    )
    select regexp_replace(phone_no,'[^[:digit:]]') phone_no
      from t
    
  • Validate with a regular Expression

    Hi Experts,


    I want some regular expressions to format number as: nn/nn/nnnn

    Expressions: total 10 digits only, and / after 4 digits, as / after 7 digits.


    Test data:

    SELECT ' 1111/18/01 ' AS NUM FROM DUAL

    UNION

    SELECT ' 01 / 01/2522 ' AS NUM FROM DUAL

    UNION

    SELECT ' 222/01/01 ' AS NUM FROM DUAL

    UNION

    SELECT ' 2222/01/1 ' AS NUM FROM DUAL

    UNION

    SELECT ' 2222/0/01 ' AS NUM FROM DUAL

    UNION

    SELECT ' 2522/01 ' AS NUM FROM DUAL

    UNION

    SELECT ' 01 / / 01' AS NUM FROM DUAL.


    Desired output

    REGEXP

    -------------

    1111/18/01

    2522/01/01

    Thanks in advance

    BANNER

    ----------------------------------------------------------------

    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - production

    I hope... This does not validate a date.

    WITH the data AS

    (SELECT "1111/18/01 ' AS NUM FROM DUAL

    UNION

    SELECT ' 01 / 01/2522 ' AS NUM FROM DUAL

    UNION

    SELECT ' 222/01/01 ' AS NUM FROM DUAL

    UNION

    SELECT ' 2222/01/1 ' AS NUM FROM DUAL

    UNION

    SELECT ' 2222/0/01 ' AS NUM FROM DUAL

    UNION

    SELECT ' 2522/01 ' AS NUM FROM DUAL

    UNION

    SELECT ' 01 / / 01' AS NUM FROM DUAL.

    )

    Select num

    FROM THE DATA

    where regexp_like(num,'^\d{4}/\d{2}/\d{2}$')

    /

    HTH

  • "Matches regular Expression" and "Model match" vi behaves differently

    Hello

    I need a simple matching chain and experimenting that found the "Regular Expression Match' and 'Correspondence model' vi behave a little differently. I guess that the entries of the regular expression on the two the same behavior. It's a difference that I discovered that the "|" character ("vertical" character, commonly used as an operator 'or') is recognized as such in the regex to Match vi, but not in the match vi model (where it is taken to the letter). Furthermore, I can't find any document using (online or in LabVIEW) on the ' | ' usage in regular expressions of character. Is - this documented anywhere?

    For example, suppose I want corresponding to one of the following 4 words: 'The' or 'fast' or 'brown' or 'fox '. The regular expression ' the | fast | Brown | Fox' (without the quotes) works for the vi Regular Expression Match but not the match pattern vi. Here is a photo of the block diagram and the results of the façade:

     

    The Help explains that the vi Regular Expression Match performs a little more slowly the match vi pattern, so I started with the latter. But since he does not work for me, I'll use the old. But does anyone have an idea of the speed difference? I guess that's negligible in a simple example.

    Thank you!

    Thank you, Jeff. That's what I was looking for. BUT my version of LabVIEW 8.5, does NOT say "functionfor example, the Match model does not support the parenthesis or vertical bars (|) characters.«» !

    See: http://zone.ni.com/reference/en-XX/help/371361D-01/glang/match_pattern/

    and http://zone.ni.com/reference/en-XX/help/371361D-01/glang/match_regular_expression/

    It is not mentioned in the help of special characters used for the match pattern : http://zone.ni.com/reference/en-XX/help/371361D-01/lvhowto/specialcharformatchpatt/

    The only place | has 'talked', it is in the sentence: "some regular expressions that use alternating (such as (. |))". \s)*) require significant resources to deal with when it is applied to the large input strings. "But I'm not processing a large chain.

    It seems that NEITHER fixed this omission. What version is your help?

    Ed

  • Divide a CSV with a regular Expression

    I'm working on a script that will read the CMYK values from a CSV file and add the nuances of the Swatches palette. I can easily split the CSV entering a comma (.split(",");), but I can't seem to get a regular expression to separate by commas and new lines.

    Here is the code snippet I have right now is not working:

    var fileIn = new File("/Users/brianp/Desktop/AVL-PMS-TEMP.csv");
    fileIn.open();
    var csvIn = fileIn.read();
    fileIn.close();
    
    var regEx = "/,|\\n/";
    csvRecords = csvIn.split(regEx);  
    

    I am barking the wrong tree here?

    I could change the CSV advance to find all line breaks and replace them with commas, but it looks more elegant to adapt my code for the CSV file is delivered.

    Thanks in advance!

    csvRecords = csvIn.split(/[,\n\r]/);

    This is the shortcut. To preset the regex, to leave out the quotation marks:

    var regEx = / | \n/ ;

    Better to add the CR (\r) as well.

    Use the classes of characters [...] If you can instead of alternatives (... |...), they are more effective (allegedly).

    Peter

  • The Regular Expressions and GUID.

    Hello gurus, I hope you can help me!
    I need to select GUID from a table and to do this, I need the regular Expression. My
    Perl is not good and not good Regular Expression. My database is Oralce 11.2.0.2.0 and
    Linux (Oracle Version 6) is the operating system of the Machine. If you need further information,
    I'll look closely. Thank you. Jehangir.

    >

    Hi Jehangir and welcome to the forums.

    I need to select the GUID of a table and to do this,

    Well, the first thing we do is read the forum FAQ and also the post by BluShadow
    at the top of the messages on the home page of the forum. You should have provided code (DDL
    and DML) showing your particular problem, but since it's your first time, I'll be gentle ;)

    We have it done - clients have sometimes GUID as PKs, and we need to send data to
    their systems, but it is not as simple as it may first appear.

    GUID may arise in three formats.

    The Oracle one - SELECT Sys_GUID() from DUAL which is just a string of 32 hexadecimal characters.
    Then the chain with the hyphen, then the string with dashes and {} at the beginning and end (see
    examples of data).

    with datax as
    (
      SELECT '79A864CCD8E44CD8B0A2765DF9EF337B' as guid  FROM DUAL  UNION ALL
      SELECT '79A864CFD8E44CD7B0A2765DF9EF337B' FROM DUAL UNION ALL
      SELECT '8gdfsgsgfdg' FROM DUAL UNION ALL  -- dummy for testing
      SELECT '21EC2020-3AEA-1069-A2DD-08002B30309D' FROM DUAL UNION ALL
      SELECT '21EC5550-3AEA-1069-A2FF-08002B30309D' FROM DUAL UNION All
      SELECT '{21CC2020-3AFA-1A69-A2DD-08002B30309D}' FROM DUAL
    )
    
    -- first one is the Oracle format
    select * from datax where regexp_like(guid, '[0-9a-fA-F]{32}'); -- Oracle select sys_guid();
    
    -- second one is with hyphens
    
    select * from datax where regexp_like(guid, '[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}');
    
    -- third one is with hyphens and curly brackets.
    
    select * from datax where regexp_like(guid, '^\{[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}\}$');
    
    -- This converts both of the last two formats back into Oracle format, which is what
    -- we use. Notice, that I haven't used regualar expressions to do this. Regexes are
    -- computationally expensive, and you should use Oracle's string furnctions if possible
    
    SELECT REPLACE(REPLACE(REPLACE(GUID, '{', ''), '}', ''), '-', '')  FROM Datax;
    

    HTH,

    Paul...

    Jehangir.

  • Form validation helps with the regular Expression [a-zA-Z]

    I'm trying to use the regular expression [a-zA-Z] to allow only upper or lowercase WITHOUT SPACES. With the help of [a-zA-Z] allows space and numbers.

    Could someone give me a point in the right direction?

    Thank you!

    RGNelson wrote:

    I'm trying to use the regular expression [a-zA-Z] to allow only upper or lowercase WITHOUT SPACES. With the help of [a-zA-Z] allows space and numbers.

    Could someone give me a point in the right direction?

    Please try with the following regular expression, which should work for text entry fields 'a line' well standard.

    ^ [A-Za-z] + $

    See you soon,.

    Günter

  • Need help with a regular expression

    I have a Zebra printer string that I want to analyze some information, but I can't get a single regex to do.

    The string is like the following:

    ↑ FT342, 695 ^ A0N, 83, 81 ^ FH\ ^ FDS/N: [[WIDGET]] ^ FS

    ^ FT793, 1170 ^ A0N, 67, 67 ^ FH\ ^ ea FD1. Widget #00123 ^ FS

    ^ FT793, 1170 ^ A0N, 67, 80 ^ FH\ ^ ea FD2. #00456 Widget Deluxe ^ FS

    ^ FT793, 1170 ^ A0N, 67, 90 ^ FH\ ^ FD #0789 ^ FS

    I want what is in red. It will vary from one label to the label. I will use the results to show to the user what label it is printing. Who is blue only varies, if that helps anything.

    Thanks for any help!

    Here would be a way using regular expressions. Again, you have to ignore the first entry and this version will add a blank entry in the last position of the table. Wil you have more entries if your label contains more than what you have posted...

  • Analyze the Mac address with the regular expression matching

    Hello world

    I have a problem with the function of regular expression matching,

    I try to analyse the response both a query arp - a 192.168.0.15 to retrieve the MAC address of the remote IP address, I used the following regular expression: ^ ([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})$

    I wonder why should I do a subset of the first string to extract only the part of the MAC address. The regular Expression function is not able to recognize the regular expression directly in the middle of a string?

    I only works when I extracted the subset of tring right as in the picture below.

    Thanks for your replies.

    Get rid of the "^" at the beginning of your regular expression. You are ordering him to find the model at the beginning of the string.

  • Tips SQL - replace one OR perhaps with a regular expression?

    Hi all

    I have a small question. This bothers me for weeks, and I was wondering if anyone of you has another idea for this.

    Imagine the following table. This is a very stupid table. But look at the sample data. I have reproduced the issue I have with the 3 files.
    CREATE TABLE departments
    (
      dpt_id NUMBER(10), 
      dpt_name VARCHAR2(100)
    );
    
    INSERT INTO departments(dpt_id, dpt_name) VALUES(1, 'NIGER');
    INSERT INTO departments(dpt_id, dpt_name) VALUES(1, 'NIGERIA');
    INSERT INTO departments(dpt_id, dpt_name) VALUES(1, 'NIGER.0001');
    
    SELECT * 
      FROM departments 
     WHERE (dpt_name = 'NIGER' OR dpt_name LIKE 'NIGER.%');
    If I use one AS % of NIGER, I recover as NIGERIA (which I don't want). So, instead, I use this GOLD combination and it works well. But I was wondering if it can be done without the GOLD.

    Thank you

    Hello

    This example gets the results you requested:

    SELECT    *
    FROM        departments
    WHERE       REGEXP_LIKE ( dpt_name
                      , '^NIGER([^[:alpha:]].*)?$'
                    )
    ;
    

    Describe the desired results. With only 3 or 4 lines of sample data, it is very easy to get good results for the wrong reasons.

  • Need help on the regular expressions and query

    Hi guru s, I hope that you all made great!

    I have a scenario where I need to insert data into the table.

    I have three scenarios:
    select 'Kodali,Raj,S' str from dual
    union 
    select 'Alex Romano' from dual
    union 
    select 'ppppp' from dual
    
    Alex Romano
    Kodali,Raj,S
    ppppp
    What I want is now.

    1 Alex Romano
    is there space between the chain and then I want to insert in the last name and first name columns
    2 Xavier, Raj, S
    If there is a comma between the chain and I want to insert in the last name, first name and middle name
    3. If there is one channel then insert even in first name and last name.

    I wrote the request more early to manage only by commas and now I'm trying but not able to use this all scenarios
    Can you please help me.
    WITH t AS (
     
    select 'Kodali,Raj,S' str from dual
    union 
    select 'Alex Romano' from dual
    union 
    select 'ppppp' from dual
    
    ) 
    select DECODE(trim(a),NULL,'a',trim(a)),DECODE(trim(b),NULL,'b',trim(b)),decode(trim(c),NULL,'c' ,trim(c)) 
    from 
     (
      SELECT max(decode(level,1,regexp_substr(str,'[^,]+',1,level))) a --INTO lFNAME
              , max(decode(level,2,regexp_substr(str,'[^,]+',1,level))) b --INTO lLNAME
              , max(decode(level,3,regexp_substr(str,'[^,]+',1,level))) c --INTO lMNAME       
       FROM   t
      CONNECT BY regexp_substr(str,'[^,]+',1,level) IS NOT NULL
      GROUP BY str
    ) ;
    Currently I put a b and c if its null value.


    Thanks in advance!

    Hello
    You can do what you asked for in pure SQL like this:

    WITH     got_pos          AS
    (
         SELECT     str
         ,     INSTR (str, ',')     AS comma_pos
         ,     INSTR (str, ' ')     AS space_pos
         FROM     t
    )
    SELECT    str
    ,       CASE
               WHEN  comma_pos > 0
               THEN  REGEXP_SUBSTR (str, '\w+', 1, 2)
               WHEN  space_pos > 0
               THEN  SUBSTR (str, 1, space_pos - 1)
               ELSE  str
           END     AS fname
    ,       CASE
              WHEN  comma_pos > 0
              THEN  REGEXP_SUBSTR (str, '\w+', 1, 3)
              ELSE  TRIM (REGEXP_SUBSTR (str, ' \w+ '))
           END     AS mname
    ,       CASE
               WHEN  comma_pos > 0
               THEN  SUBSTR (str, 1, comma_pos - 1)
               ELSE  REGEXP_SUBSTR (str, '\w+$')
           END     AS lname
    FROM       got_pos
    ORDER BY  str
    ;
    

    But look how it is difficult, even for this simple example of data.

    I suggest you write a PL/SQL function to analyze the name. It will be much easier to deal with a combination of spaces and commas, the names of more than 3 words, etc.

  • stuck with a regular Expression

    Clear the javascript by RegExp train file comments: [/] [*]. + [*] [/]
    It s nice work with comments like this:
    / * blah-blah-blah * /.
    But what about like this:
    /*
    bla
    bla
    bla
    */
    How can I 'say' - ANYTHING?

    Fuffel wrote:
    > Tent clear the javascript by RegExp file comments: [/] [*]. + [*] [/]
    > It s nice work with comments like this:
    > / * blah-blah-blah * /.
    > But what about like this:
    > /*
    > blah
    > blah
    > blah
    > */
    > How I can 'say' - ANYTHING?
    >
    In find/replace in DW, it seems to work, but test it:

    \/\*[\w\W]*?\*\/

    --

    E. Michael Brandt

    www.divaHTML.com
    www.divahtml.com/Products/scripts_dreamweaver_extensions.php
    According to the standard scripts and the Dreamweaver Extensions

    www.valleywebdesigns.com/vwd_Vdw.asp
    JustSo PictureWindow
    JustSo PhotoAlbum, and alia

    --

  • Mask a number with regular expressions

    Hi @ll!

    Is it possible to hide a given number

    of "12345678" in "XXXX5678".
    or '987458' to 'XX7458 '.

    with Regular Expressions and without substr()? To display only the last four digits and the 'X' value for the rest. The size of the number is not always the same.

    Alex, I think the OP wanted the first 4 characters to hide ;)

    Something like that?

    select translate(substr('12345678',1,length('12345678')-4),'1234567890','XXXXXXXXXX')
                    || substr('12345678',length('12345678')-3)
              from dual
    /
    TRANSLAT
    --------
    XXXX5678
    

    More

    with test_data as
    (
    select '12345678' card_no from dual
    union all select '123456' from dual
    union all select '5678900' from dual
    ) -- End of test data
    select translate(substr(card_no,1,length(card_no)-4),'1234567890','XXXXXXXXXX') ||
              substr(card_no,length(card_no)-3)
        from test_data
    /
    
    TRANSLATE(SUBSTR
    ----------------
    XXXX5678
    XX3456
    XXX8900
    

    Arun-

Maybe you are looking for

  • Upgraded to Windows 7 and no sound from monitor speakers integrated

    I upgraded my desktop HP Pavilion Media Center a1450n PC to Windows 7 successfully except for sound. My speakers are integrated into my screen LCD 17-inch HP vs17e and Windows 7 can not detect my built in speakers, I downloaded most every sound drive

  • Search for alternative without GSO to 3D Picture Control

    Hello! By default, the 3D picture control is broken/non functional on Dell laptops that my company to buy things.  All these notebooks have Intel HD Graphics chipsets. Disabling the device driver Intel (in Device Manager) corrects the image 3D contro

  • What happened to the Windows Defender Update page?

    When I try to update Windows Defender, the regular update page does not appear.  Another page with many available downloads appears, and there is no link or even mention of Windows Defender.

  • HttpConnection questions in discussions

    I have a requirements where my blackberry app to upload & download files from the server. To improve the upload and download performance, I am start several threads and send multiple pieces of files through multiple connections. My server is able to

  • Table full page

    Good afternoonI wanted to take my picture on the entire page. In the collection of panels contained AFStrechWidth styleClass and inline style height value: 655 px.The problem of the use of a physical height, i.e. when I ADVANCED (which causes the NR