1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.
  2. Better Analytics for WordPress Get It Free

Srting related help needed

Discussion in 'PHP' started by Jeehan, Apr 14, 2012.

  1. #1
    Hello,

    Need help, please help me out:

    a string is "test-5248-87dd133a6d" or "abc-def-5248-87dd133a6d". I am trying to take out only the "test" or abc-def" from the string.


    Or Trying to remove "-5248-87dd133a6d" part from the string. rem: -5248- is constant, "-87dd133a6d" is variable.

    Thank you.
     
    Solved! View solution.
    Jeehan, Apr 14, 2012 IP
  2. #2
    Assuming that your string is in a variable called $s...

    $s = "test-5248-87dd133a6d";
    $endPos =
    strpos($s, "-5248-87dd133a6d");
    $s = substr($s, 0, $endPos);

    For more info on how this works, do a Google search on "php strpos" and "php substr".

     
    mikeweller, Apr 14, 2012 IP
  3. Basti

    Basti Active Member

    Messages:
    627
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    90
    #3
    this should work, 2 methods

    
    $string = "test-5248-87dd133a6d";
    
    // Remove all after test
    // Simply find -5248- and any character after it and remove it
    $new_string = preg_replace('/-5248-.*/', '', $string);
    
    // Remove all before constant -5248-
    // Match every character ( . ) and repeat ( * ) as long as its followed by a given string, but dont include string in match ( ?=string ) This is a posibive lookahead
    $new_string = preg_replace('/.*(?=-5248-)/', '', $string);
    
    Code (markup):
     
    Basti, Apr 14, 2012 IP
  4. Jeehan

    Jeehan Well-Known Member

    Messages:
    1,578
    Likes Received:
    29
    Best Answers:
    1
    Trophy Points:
    115
    #4
    Thank you guys. Both worked fine. Thank you again.
     
    Jeehan, Apr 14, 2012 IP