I have a string that contains variables in it, and I want to add the values of the variables into it, as I'm trying it, it simply spits out as a string instead of adding the values of the variables.
Here is my code
$vars = $item->toArray();
extract($vars);
echo ($message_tmpl);die;
echo print_r($message_tmpl)die;
Variables are extracted to add the values, but it returns plain output instead of values.
$first_name is extracted through $vars
Output
'My message to $first_name';
It should be
'My message to John Doe';
thanks
you can do this :
$name = "Toto";
$info["age"] = "8yo";
echo "Hello {$name} who is {$info["age"]}";
will output :
Hello Toto who is 8yo
You can also use the strtr() function such as :
$template = '$who likes $what';
$vars = array(
'$who' => 'Toto',
'$what' => 'fruits',
);
echo strtr($template, $vars);
you will get :
Toto likes fruits
Related
Totally lost on how to solve. I have a mysql text record which contains the following text:
Hello $someone
When I output it in php, it shows as Hello $someone - which is fine and what I expect.
However, how can I output it in php so that it turns $someone into a php variable, which is assigned in php?... So I'd like my php code like:
$someone = "John Doe";
echo $subject;
returns: Hello John Doe
I've tried looking at variable variables, using $$someone, ${$someone} but always just returns text.
I understand that $someone would always be a text, so would have to have it stored in mysql as something like {$someone} to differentiate it from a dollar amount like $50 etc.
Any help would be greatly appreciated!
Here's an exampe of a simple function that will replace placeholder values in a string. Both the strings ($text) and the data could easily come from the database.
$text = "";
$text .= "<p>Dear {name},</p>\n";
$text .= "<p>Thank you for your order on {order_date}.</p>\n";
$text .= "<p>You order was shipped on {ship_date}.</p>\n";
$data = array(
"name" => "John Doe",
"order_date" => "05/01/2018",
"ship_date" => "05/04/2018",
"order_total" => "$22.50"
);
$textToDisplay = curly_replacer($text,$data);
echo $textToDisplay;
// Function to replace placeholders with data values.
// $str contains placeholder names enclosed in { }
// $data is an associative array whose keys are placeholder,
// and values are the values to replace the placeholders
function curly_replacer($str,$data) {
$rslt = $str;
foreach($data as $key => $val) {
$rslt = str_replace("{".$key."}", $val, $rslt);
}
return $rslt;
}
I am very new in PHP and I was checking whether all my controlers are in so how can I echo this? what I tried resulting nothing
$controllers = array_intersect($json_api->get_controllers(), $active_controllers );
$return = array(
'json_api_version' => $version,
'controllers' => array_values($controllers)
);
echo $return['controllers']['controllers'];
use print_r function:
print_r($return['controllers']);
when you want read city you do:
$arr_controllers = $return['controllers'];
$key_2 = $arr_controllers[2];
where 2 is the key
If you want to print the values of an array with a
understandable format you should use print_r() instead of echo like so:
print_r($return['controllers']);
You can also use var_dump() to get some extra information about the fields, like the type and lenght.
If what you need is to asign a certain index of the array to a value just do something like this:
$variable = $return['controllers'][indexOfField]; // indexOfField=2 for city field
echo $variable;
For further information about print_r() check the official manual.
I am creating a function to parse text from a templating system, and add the corresponding values.
For example, the user might input hi [[first_name]] and the [[first_name]] part will be replaced with the actual first name.
Somehow, I parsed that and ended up with a text that looks like this:
hi $info['first_name']
The above is just as text though, what can I do to actually make $info['first_name'] be the value (I already have that array in there, but I am not sure how to convert string to PHP variable)
Thanks!
Use simple str_replace function:
$str = "hi [[first_name]]";
foreach (array_keys($info) as $key) {
$str = str_replace("[[".$key."]]", $info[$key], $str);
}
echo $str;
str_replace("[[first_name]]", $info['first_name'], 'hi [[first_name]]');
You haven't share your code but you may print the variable name instead of its value.
<?php
$myTemplate = "hi [[first_name]], how are you this fine [[day_of_week]]?";
$myData = array(
'[[first_name]]' => 'James'
,'[[day_of_week]]' => 'Friday'
);
echo str_replace(array_keys($myData), array_values($myData), $myTemplate);
?>
On my php page, I am getting the follow output:
Array ( [contact/email] => users_name#email_address.com )
This is produced via the following line in the php code:
print_r($openid->getAttributes());
How do I extract the text users_name#email_address.com from that array and stick it into a variable $strEmail;?
So when I echo the variable:
echo $strEmail;
It should output the following on screen:
users_name#email_address.com
Assign the array to a variable and then you can easily access it:
$attributes = $openid->getAttributes();
$strEmail = $attributes['contact/email'];
echo $strEmail; // => users_name#email_address.com
In your specific case, you can do:
$strEmail = reset($openid->getAttributes());
However it's better to suggest as this will work for other cases,too:
$attributes = $openid->getAttributes();
$strEmail = $attributes['contact/email'];
For more details see the PHP Manual about arrays how to access them.
I have a function that takes the input of a user defined string and an array of data (key=>value), which looks like this;
$text = "Hi! My name is #name, and I live in #location.";
$dataArray = array("name" => "Mikal", "location" => "Oslo, Norway");
function MakeString($text, array $dataArray)
{
// return manipulated string...
}
I would like my function to swap the string #variables with data from the array, where string-variable matches array-key (if it does), so that the function returns:
"Hi! My name is Mikal, and I live in Oslo, Norway."
foreach($dataArray as $key=>$value)
{
$text= str_replace("#".$key,$value,$text);
}