1

I've the string block:

Size : Size 13 - 950-975 <br />Easygrip_Locking_System : No <br />Veranda_pole : No <br />Safelock_tie_down : No <br />Storm_Pole : No <br />Tall_Annexe : Yes <br />Inner_tent : Yes <br />

What I need is to create variables from the above block like :

$Size = "Size 13 - 950-975";
$Easygrip_Locking_System = "No";
$Veranda_pole = "No";
...
$Inner_tent = "Yes";

All of this are product options and depending on a product type possible to be more ore less and different.

I've already tried with preg_split than to put in array and eval() but not get what I need.

$keywords = preg_split('/[\,]+/', '$Size= "Size 13 - 950-975";,$Easygrip_Locking_System= "No";');
print_r($keywords);
3
  • How about prepare the string to work with parse_str()? Commented Apr 2, 2018 at 10:10
  • you need to restrict some pattern to achieve with explode,trim and format variable with php Commented Apr 2, 2018 at 10:14
  • Do not do this it is bound to cause problems, create an array of values rather than actual variables. Commented Apr 2, 2018 at 10:51

3 Answers 3

3

You can achieve this by exploding your string in to array then separate variable and value by :

$str = "Size : Size 13 - 950-975 <br />Easygrip_Locking_System : No <br />Veranda_pole : No <br />Safelock_tie_down : No <br />Storm_Pole : No <br />Tall_Annexe : Yes <br />Inner_tent : Yes <br />";
$arr =explode("<br />",$str);
var_dump($arr);
foreach ($arr as $key => $value) {
  $tmp = explode(":", $value);
  if(count($tmp) > 1)
    ${trim($tmp[0])} = trim($tmp[1]);
}

//Here you can get your variables like:

echo $Size;
Sign up to request clarification or add additional context in comments.

2 Comments

You need to trim() the $tmp variables otherwise you could get unwanted spaces in the variable names and values.
Declaring $key is not necessary.
2

Prepare the string with appropriately placed = and &, then parse, then extract.

$string="Size : Size 13 - 950-975 <br />Easygrip_Locking_System : No <br />Veranda_pole : No <br />Safelock_tie_down : No <br />Storm_Pole : No <br />Tall_Annexe : Yes <br />Inner_tent : Yes <br />";
$string=str_replace([' : ', ' <br />'], ['=', '&'], $string);
parse_str($string, $out);
extract($out);

echo "Size = $Size\n";
echo "Easygrip_Locking_System = $Easygrip_Locking_System\n";
echo "Veranda_pole = $Veranda_pole\n";
echo "Safelock_tie_down = $Safelock_tie_down\n";
echo "Storm_Pole = $Storm_Pole\n";
echo "Tall_Annexe = $Tall_Annexe\n";
echo "Inner_tent = $Inner_tent\n";

Output:

Size = Size 13 - 950-975
Easygrip_Locking_System = No
Veranda_pole = No
Safelock_tie_down = No
Storm_Pole = No
Tall_Annexe = Yes
Inner_tent = Yes

Demo: https://3v4l.org/ppv3G

If you want to perform validation or sanitize or make any other preparations, I recommend doing so before calling extract.

Alternatively, if you want to have greater control of the key and value substrings via a regex pattern, you can use pre_match_all().

if (preg_match_all ('~([a-z_]+) : (.+?) <br />~i', $string, $out, PREG_PATTERN_ORDER)) {
    extract(array_combine($out[1], $out[2]));

    echo "Size = $Size\n";
    echo "Easygrip_Locking_System = $Easygrip_Locking_System\n";
    echo "Veranda_pole = $Veranda_pole\n";
    echo "Safelock_tie_down = $Safelock_tie_down\n";
    echo "Storm_Pole = $Storm_Pole\n";
    echo "Tall_Annexe = $Tall_Annexe\n";
    echo "Inner_tent = $Inner_tent\n";
}

Demo: https://3v4l.org/vjYiK

3 Comments

It's probably safer to preg_replace than str_replace in case the white space varies.
That needs to be clarified by the OP. Providing multiple input samples should clarify any challenges.
@misi I've posted two clean approaches that do not use variable variables. If you do not NEED to declare the substring pairs as individual variables, I recommend just using the array element keys to access the values. I've never had the need to use extract in any of my production codes. If this solves your question, please award the green tick so that your answer is deemed resolved. If you would like clarification on something just ask.
0
$str = 'Size : Size 13 - 950-975 <br />Easygrip_Locking_System : No <br />Veranda_pole : No <br />Safelock_tie_down : No <br />Storm_Pole : No <br />Tall_Annexe : Yes <br />Inner_tent : Yes <br />';

$result = array();

foreach (explode('<br />', trim($str, '<br />')) as $key => $value) {
    $key = trim(strstr($value, ':', true));
    $value = trim(substr(strstr($value, ':'), 1));
    $result[$key] = $value;
}

// May be unsafe    
extract($result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.