gdfhghjdfghgfhf Posted May 9, 2008 Share Posted May 9, 2008 hello, just one simple question i have a variable that contains stuff like this: $var = "cat - dog - fish" i'd like to keep only the part between the first " - " the result i'd want from the example above would be just "cat" thanks! Link to comment https://forums.phpfreaks.com/topic/104945-regex-help/ Share on other sites More sharing options...
oliverw92 Posted May 9, 2008 Share Posted May 9, 2008 /\b(.+) \-.+/ or if its just letters /\b([a-Z]+) \- .+/ Link to comment https://forums.phpfreaks.com/topic/104945-regex-help/#findComment-537181 Share on other sites More sharing options...
Caesar Posted May 9, 2008 Share Posted May 9, 2008 <?php $str = 'cat - dog - fish'; $animal= explode('-',$str); $cat = trim($animal[0]); ?> <?php $str = 'cat - dog - fish'; preg_match('([aA-zZ]+) \-',$str, $animal); $cat = $animal[0]; ?> Link to comment https://forums.phpfreaks.com/topic/104945-regex-help/#findComment-537184 Share on other sites More sharing options...
soycharliente Posted May 10, 2008 Share Posted May 10, 2008 I like the explode better Seems easier to understand what's going on to me. <?php $list = explode(" - ", $string); $choice = $list[0]; ?> Link to comment https://forums.phpfreaks.com/topic/104945-regex-help/#findComment-537213 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.