0

I am pulling addresses from the database, which is currently stored as a string in the database and these are the values received.

string(175) "a:5:{s:14:"street_address";s:23:"230 Sherwood Forest Rd.";s:12:"address_line";s:0:"";s:4:"city";s:13:"Winston Salem";s:5:"state";s:15:"North Carolina ";s:3:"zip";s:5:"27104";}"

string(149) "a:5:{s:14:"street_address";s:14:"53 East Street";s:12:"address_line";s:0:"";s:4:"city";s:10:"Middletown";s:5:"state";s:2:"CT";s:3:"zip";s:5:"06457";}"

string(157) "a:5:{s:14:"street_address";s:25:"5625 Brownstone Circle NW";s:12:"address_line";s:0:"";s:4:"city";s:6:"Canton";s:5:"state";s:4:"Ohio";s:3:"zip";s:5:"44718";}"

Goal

I need to loop through each address and create dynamic variables, based on their values. For example from the first address returned: I would like to create the variables:

  1. $streetAddress = "230 Sherwood Forest Rd."
  2. $city = "Winston Salem"
  3. $state = "North Carolina"
  4. $zip = "27104"

What I've Tried and/or researched

Similar Stack Overflow Questions:

substring of variable length

Grab specific value from string with PHP

How to extract a string from double quotes?

I have also tried exploding the string on double quotes and pushing into an array and trimming.

I have also tried several variations of preg_match() and preg_match_all() and then storing in an array.

$str = 'a:5:{s:14:"street_address";s:23:"230 Sherwood Forest Rd.";s:12:"address_line";s:0:"";s:4:"city";s:13:"Winston Salem";s:5:"state";s:15:"North Carolina ";s:3:"zip";s:5:"27104";}';
preg_match('/:".*.";/', $str, $match);
echo $match[0];

Question

How do I make sure that the value I'm grabbing, such as "street_address", is actually the "street_address" and return the correct value within the double quotes?

Man of Progress
  • 103
  • 1
  • 14

0 Answers0