bandito Posted September 18, 2011 Share Posted September 18, 2011 Hey guys, I am trying to return some data between 2 delimiters. For example I have sdfkasdjfk address: 1 smith road: asdkfasdjf and I want to be able to return address: 1 smith road: and replace it with an empty string. The information between the address: and : is always different. I have played around with preg_split(), preg_grep(), preg_replace but have had no luck. Any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
xyph Posted September 18, 2011 Share Posted September 18, 2011 Need more accurate sample data Quote Link to comment Share on other sites More sharing options...
xyph Posted September 18, 2011 Share Posted September 18, 2011 Nevermind, reread and I think I figured it out /address: ([^:])/ $expr = '/address: ([^:]++)/'; $str = 'sdfkasdjfk address: 1 smith road: asdkfasdjf'; preg_match( $expr, $str, $match ); print_r( $match ); use preg_match_all() if you have multiple results you want to return Quote Link to comment Share on other sites More sharing options...
bandito Posted September 18, 2011 Author Share Posted September 18, 2011 That's pretty much what I want but I was hoping to replace the match with and empty string. I managed to do using preg_match() with a combination of substr() and strlen(). Thank you very much your example works great. Quote Link to comment Share on other sites More sharing options...
xyph Posted September 18, 2011 Share Posted September 18, 2011 Instead, use preg_replace() <?php $expr = '/address: [^:]++:/'; $str = 'sdfkasdjfk address: 1 smith road: asdkfasdjf'; $str = preg_replace( $expr, '', $str ); echo $str; ?> Quote Link to comment Share on other sites More sharing options...
bandito Posted September 20, 2011 Author Share Posted September 20, 2011 Thanks xyph! I ended up using booth and they work much better than what I had. One question, in the regular expression for the work "address" , what would i need to do to see if there is 1 or more letter "d" or letter "s"? Thanks again those regular expressions work great! Quote Link to comment Share on other sites More sharing options...
xyph Posted September 20, 2011 Share Posted September 20, 2011 So you want it to match 'adres' or 'addres' or 'adress' as well? /add?res?s: [^:]++:/ The ? means 'match previous character/group/class between zero and one times, as many times as possible, giving back as needed (greedy)' Quote Link to comment 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.