Hello i have a script that extract company names from a string. I want that the extracted names to be converted to php variable. So for example first result Real Coffee Sweeden must be converted to $RealCoffeeSweeden = 0
so i can assign a value to it
$test='/showname/0406741848 : Real Coffee Sweeden
/showname/0406741849 : Healthzone SE
/showname/16133663413 : FREE
/showname/16133663414 : RadioPlantes Canada
/showname/16133663417 : Dealsoftoday.Eu Canada
/showname/16136995593 : FREE
/showname/16136995594 : Club White Smile Canada
/showname/16138007442 : FREE
/showname/16138007547 : Mybitwave.com Canada
/showname/16465596150 : BABY
/showname/16465696956 : FREE
/showname/16465696957 : FREE
/showname/16467419944 : Mybitwave.com UK
/showname/16469181975 : FREE
/showname/21501350 : SecureSurf.EU NO
/showname/21501351 : FileCloud365 Norwegian
/showname/21501352 : FREE
/showname/21501353 : RadioPlantes Norwegian
';
$myRows= explode("
", $test);
foreach( $myRows as $key => $value) {
$pieces = explode(":", $value);
$result[] = $pieces[1];
}
foreach ($result as $res){
$res // covert to php variable
//example: $RealCoffeeSweeden = 0;
}
You should use an array for that. But if you want to do it the way you write, you can simply do something like that:
foreach( $myRows as $key => $value) {
$pieces = explode(":", $value);
$res = str_replace(' ', '', $pieces[1]); // replace whitespaces for valid names
$$res = 0; // note the double dollar signs
}
If you want to use an array tho, do something like this:
$result = [];
foreach( $myRows as $key => $value) {
$pieces = explode(":", $value);
$key = str_replace(' ', '', $pieces[1]);
$result[$key] = 0;
}
According to your comment, change second last line in the foreach loop with following:
$res = str_replace(' ', '', $res) . '_f';
You can try it this way
$my_array = explode("
", $test);
foreach($my_array as $key => $value {
$my_string = explode(':', $value)
${str_replace(' ','', $my_string[1])} = $my_string;
echo $$my_string;
}