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.
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".
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):