tserfer Posted June 18, 2013 Share Posted June 18, 2013 Hello, I hate these preg_matches but this seems easy one I have the following string $string = "name=john; [email protected]; phone=4234234;"; I want to preg_match this $string to take our the keys name, email, phone and their values john, [email protected] and 4234234; What is special about this is that the string may not contain the character ; or it may have spaces between key and value. For example all the possible combinations are the followings. $string = "name=john; [email protected]; phone=4234234;"; #primary $string = "name=john; [email protected]; phone=4234234"; #it doesnt have for example the ; at the last one $string = "name =john; email [email protected]; phone =4234234;"; #it has a space before the char = $string = "name = john; email = [email protected]; phone = 4234234;"; #it has a space after and before the = $string = "name= john;email= [email protected];phone=4234234;"; #it can be like that, no spaces everywhere I hope you understand me. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/279308-preg_match-help/ Share on other sites More sharing options...
AbraCadaver Posted June 18, 2013 Share Posted June 18, 2013 (edited) For something this easy you don't need regex. htmlentities() may not be needed but just to be sure: $string = str_replace(';', '&', str_replace(' ', '', htmlentities($string))); parse_str($string, $values); print_r($values); Edited June 18, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/279308-preg_match-help/#findComment-1436651 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.