Removing a 4 digits number from string

Discussion in 'PHP' started by redbayron2006, Nov 1, 2008.

  1. #1
    Hi there guys, I want to remove a random 4 digit number from a string. For example let say we have:
    $str = "1) Hallo 1536 world!";
    $str2 = "2) Hallo to all of my 4728 friends!";

    I cant go removing digits between 0-9 cause 1) and 2) will be erased. I want just to remove every number that has 4 digits only.
    Any suggestions?
    Thanks in advance!

    (Probably preg_match ?)
     
    redbayron2006, Nov 1, 2008 IP
  2. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    
    $str = "1) Hallo 1536 world!";
    $str = preg_replace('#[0-9]{4}#','',$str);
    echo $str;
    
    PHP:
     
    Sillysoft, Nov 1, 2008 IP
  3. redbayron2006

    redbayron2006 Active Member

    Messages:
    173
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #3

    Ididnt see that coming :( The problem is that i want to remove some strings like:
    blablabla &# 8217; blablablablablabla
    blablabla &# 8220; blablabla
    blablabla &# 8221; blablablablablabla
    ........ and so on
    so how am i going to write that script above?

    $str = preg_replace('&# #[0-9]{4}#;','',$str);

    I dont believe that is going to work
    any suggestions?
     
    redbayron2006, Nov 1, 2008 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Why not? It removes all 4 digit numbers
     
    ads2help, Nov 1, 2008 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    Are you just going for HTML entities ?

    $str = preg_replace('/&#\d{4,};/', '', $str);
    Code (markup):
     
    joebert, Nov 1, 2008 IP
  6. redbayron2006

    redbayron2006 Active Member

    Messages:
    173
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Yes i just want to remove html entities, the above string will work for all of em?
     
    redbayron2006, Nov 2, 2008 IP
  7. redbayron2006

    redbayron2006 Active Member

    Messages:
    173
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Nope there are entities with 2-4 digits in them. So how can we transform the above code to remove from 2 to 4 digits instead of only 4?
     
    redbayron2006, Nov 2, 2008 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #8
    Change the 4 to a 2.
     
    joebert, Nov 2, 2008 IP
  9. Gatorade

    Gatorade Peon

    Messages:
    2,130
    Likes Received:
    222
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It's "hello" not "hallo"...
     
    Gatorade, Nov 2, 2008 IP
  10. redbayron2006

    redbayron2006 Active Member

    Messages:
    173
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #10
    lol yeap thank you but im not from an English spoken country and my real problem is the above script. Thanks anyway
     
    redbayron2006, Nov 3, 2008 IP
  11. redbayron2006

    redbayron2006 Active Member

    Messages:
    173
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #11
    I understand that. Isnt there a way not to write a 3 lines of code like:
    $str = preg_replace('/&#\d{4,};/', '', $str);
    $str = preg_replace('/&#\d{3,};/', '', $str);
    $str = preg_replace('/&#\d{2,};/', '', $str);
    Code (markup):
    but just one to make the work of the three of them?
     
    redbayron2006, Nov 3, 2008 IP
  12. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #12
    I do not understand very well but your 3 lines of codes are actually equivalent to

    $str = preg_replace('/&#\d{2,};/', '', $str);
    PHP:
     
    ads2help, Nov 3, 2008 IP
  13. redbayron2006

    redbayron2006 Active Member

    Messages:
    173
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #13


    Yeap your are right, im noob. It works fine, thanks guys for all the help!
     
    redbayron2006, Nov 3, 2008 IP
  14. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #14
    I didn't mean that....
     
    ads2help, Nov 3, 2008 IP