scarhand Posted August 14, 2008 Share Posted August 14, 2008 say i have a string like this: "joe samson is an elf - rpg gamers" and i want to get "joe samson" and "an elf" out of the string so $name = "joe samson" $class = "an elf" now the name and the class will vary. also, they could be more than 1 or 2 words in length each. i need help extracting this: "(a) is (b) - rpg gamers" $name = (a) $class = (b) would preg_replace be best for this? can i get some examples? Quote Link to comment https://forums.phpfreaks.com/topic/119583-solved-would-preg_replace-be-easiest-for-this/ Share on other sites More sharing options...
DarkWater Posted August 14, 2008 Share Posted August 14, 2008 preg_match(). <?php $matches = array(); $str = "joe samson is an elf - rpg gamers"; preg_match('$([a-z ]+) is ([a-z ]+) - rpg gamers$i', $str, $matches); print_r($matches); Quote Link to comment https://forums.phpfreaks.com/topic/119583-solved-would-preg_replace-be-easiest-for-this/#findComment-616111 Share on other sites More sharing options...
scarhand Posted August 14, 2008 Author Share Posted August 14, 2008 thank you sir Quote Link to comment https://forums.phpfreaks.com/topic/119583-solved-would-preg_replace-be-easiest-for-this/#findComment-616115 Share on other sites More sharing options...
DarkWater Posted August 14, 2008 Share Posted August 14, 2008 Any time. Quote Link to comment https://forums.phpfreaks.com/topic/119583-solved-would-preg_replace-be-easiest-for-this/#findComment-616117 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.