Jump to content

Recommended Posts

Hello,

 

I hate these preg_matches but this seems easy one

 

I have the following string

 

$string = "name=john; email=test@test.com; phone=4234234;";

 

I want to preg_match this $string to take our the keys name, email, phone and their values john, test@test.com 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=test@test.com; phone=4234234;"; #primary

$string = "name=john; email=test@test.com; phone=4234234"; #it doesnt have for example the ; at the last one

$string = "name =john; email =test@test.com; phone =4234234;"; #it has a space before the char =

$string = "name = john; email = test@test.com; phone = 4234234;"; #it has a space after and before the =

$string = "name= john;email= test@test.com;phone=4234234;"; #it can be like that, no spaces everywhere

 

I hope you understand me.

Thank you

Link to comment
https://forums.phpfreaks.com/topic/279308-preg_match-help/
Share on other sites

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 by AbraCadaver
Link to comment
https://forums.phpfreaks.com/topic/279308-preg_match-help/#findComment-1436651
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.