SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Perl Compatible Regular Expressions with PHP

    How can i use Perl Compatible Regular Expressions with PHP to find a string of numbers and add a comma to the end of it.

    For example lets say i have the following cvs file i am parseing

    fred, 234 5th ave, huntington, ny, 11746
    ray, 9 horton st, melville, ny 11747


    I need to parse this data find the numbers without any alpha characters and add a comma at the end of last number. in my script i am assigning the 2nd field $address

    result i need is as follows.

    fred, 234, 5th ave, huntington, ny, 11746
    ray, 9, horton st, melville, ny 11747



    Thanks guys/gals

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $string 
    'fred, 234 5th ave, huntington, ny, 11746
    ray, 9 horton st, melville, ny 11747'
    ;
    $string preg_replace('/(\d+) /''$1, '$string);
    echo 
    $string;
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the quick reply however, my post was not clear enough.

    After parsing $address and finding the digits i need to split $address into two varibles.
    $bn and $address.

    for example my data is as follow

    fred, 10 tower st, huntington

    $name
    $address
    $city

    after parseing the file i need to end up with 4 varibles.

    fred, 10, tower st, huntington
    $nane
    $bn
    $address
    $city


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •