iAreCow Posted November 22, 2009 Share Posted November 22, 2009 Hey there, Im facing a (quite simple?) problem, I have a string, and I'd like to extract letters from it till a certain letter (similar to substr, but it only allows extracting from and to) $word = "This is the sentence"; Lets say I'd like to extract till letter "e", so it would return "This is th" Regards Quote Link to comment https://forums.phpfreaks.com/topic/182489-function-similar-to-substr/ Share on other sites More sharing options...
Garethp Posted November 22, 2009 Share Posted November 22, 2009 preg_match('~^(.*?)e~', $word, $Matches); $word = $Matches[1]; Quote Link to comment https://forums.phpfreaks.com/topic/182489-function-similar-to-substr/#findComment-963159 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 Garethp's suggestion is certainly viable providing there is always going to be an 'e' in the input, if there isn't it won't match anything, but maybe thats what you want? If you want the whole string if no e is matched you could use... preg_match('~^(.*?)(?:e|$)~', $input, $Matches); echo $Matches[1]; Quote Link to comment https://forums.phpfreaks.com/topic/182489-function-similar-to-substr/#findComment-963162 Share on other sites More sharing options...
Daniel0 Posted November 22, 2009 Share Posted November 22, 2009 strtok($word, 'e'); No need for regex. Quote Link to comment https://forums.phpfreaks.com/topic/182489-function-similar-to-substr/#findComment-963165 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.