SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: replace numbers

  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace numbers

    Hi,

    I have a content like
    Code:
    1 jim
    2 jill 21 advance
    3 joe 1221 paid
    4 john
    I have to replace the above as

    Code:
     jim
     jill 21 advance
     joe 1221 paid
     john
    i.e i have to remove the index numbers, i use preg_replace(), don't know the pattern please help.

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $output preg_replace('/^\d+ /m'''$input); 

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    i tried it but it prints the $input and not the output, i have checked it twice it does not works, could you please explain what the above expression does?

    Code:
    while (!feof($op)) {
      $data = fgets($op, filesize($fn));
      $output = preg_replace('/^\d+ /m',' ',$data); 
    echo "$output---";
    the file has details data.

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $input = <<<IN
    1 jim
    2 jill 21 advance
    3 joe 1221 paid
    4 john
    IN;

    $output preg_replace('/^\d+ /m'''$input);

    echo 
    '<pre>'$output
    Output:

    Code:
    jim
    jill 21 advance
    joe 1221 paid
    john
    As you can see it does work. The problem is somewhere else in your code.

    What the RegEx does is look at begginning of each line for any number of digits followed by a space and replace it with an empty string (ie. remove it).

  5. #5
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks friend, This syntax worked for me
    Code:
    preg_replace('#^\d+#', '', $con);


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
  •