SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict limepickle's Avatar
    Join Date
    Jan 2006
    Location
    Aldridge UK
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple regular expression question

    I need to remove everything up to and inlcuding the -, ie the following string

    15e-6812

    needs to be converted to 6812.

    Code:
    $string  = '15e-6812'; 
    
    preg_replace($reg_expression_goes_here, '', $string);
    And the number of characters before the minus sign varies, so is it possible to match 'to the left of and including' the minus sign?

    Thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use this:

    PHP Code:
    $string  '15e-6812'

    $newstring explode('-'$string);

    echo 
    $newstring[1]; 
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $string  '15e-6812';
    echo 
    preg_replace('/^.*?-/'''$string); 

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    surely using explode in this instance would use less resources?
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  5. #5
    SitePoint Addict limepickle's Avatar
    Join Date
    Jan 2006
    Location
    Aldridge UK
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, very helpful!!

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, however preg_replace() will also make the variable a clear string, and not an array as explode() does. It really depends on what your preference is and how you want the variable to be used.

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webnoob View Post
    surely using explode in this instance would use less resources?
    If you're writing space shuttle navigation program in php, you should use strtok because it's the least resource intensive.

    PHP Code:
    $string  '15e-6812';
    strtok($string'-');
    echo 
    strtok('-'); 
    In all other cases, regexps are good enough.


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
  •