pastet89 Posted January 18, 2009 Share Posted January 18, 2009 Hello all! I need some help: I have this string: $string = "a.whatever.could.stay.here.b"; I need by having just "a" and "b" to delete all the string and to have finally: $final_string = ""; On the place of ".whatever.could.stay.here." there could stay really everithing - " ", ",", "." "?", "'", "[", "/", every letter, every number - EVERYTHING. Just the beginning "a" and the end "b" are defined and known. I know that here I can use something like preg_replace ( mixed $pattern , mixed $replacement , mixed $subject) but I don't know how to define the $pattern part of the function and I am not absolutely sure that this function will do something here... Can you help me? ??? Quote Link to comment https://forums.phpfreaks.com/topic/141323-preg_replace/ Share on other sites More sharing options...
nrg_alpha Posted January 18, 2009 Share Posted January 18, 2009 EDIT.. oops..nevermind, I missread that first part.. you want to wipe out all strings that start with a and end with b? $string = "a.whatever.could.stay.here.b"; $finalString = preg_replace('#^a.*?b$#', '', $string); Quote Link to comment https://forums.phpfreaks.com/topic/141323-preg_replace/#findComment-739700 Share on other sites More sharing options...
nrg_alpha Posted January 18, 2009 Share Posted January 18, 2009 Come to think of it, I suppose even quicker would be: #a.*b# (one of the rarer cases where .* is acceptable, as opposed to making it a lazy quantifyer [also negates the need for ^ or $..don't know what I was thinking there...]) Quote Link to comment https://forums.phpfreaks.com/topic/141323-preg_replace/#findComment-739781 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.