This question already has answers here:
How to expand variables in a string
(7 answers)
Closed 8 years ago.
I have written this,
<?php
function get_pounds($var){
$string ='i have $var pounds';
return $string;
}
$v =100;
echo get_pounds($v);
?>
outputs:
i have $var pounds
I know I can use "" or $string ='i have'. $var .'pounds'; to avoid this problem. But take a situation when fetching string from a database. I want it to parse it and make $var as a variable on its own without being part of the string.
say
variable
I have $var pounds
My query goes "select variable from $table;"
return mysql_query($query);
This will return a string.
How do I parse it so that my $var is not parsed as a var ?
instead of $string ='i have $var pounds'; try $string ="i have $var pounds";
Related
This question already has answers here:
what is the efficient way to parse a template like this in php?
(3 answers)
str_replace() with associative array
(5 answers)
PHP - Replacing multiple sets of placeholders while looping through arrays
(1 answer)
How do I use preg_replace in PHP with {{ mustache }}
(1 answer)
How can I replace a variable in a string with the value in PHP?
(13 answers)
Closed 19 days ago.
How to write custom php function for replacing variable with value by passing function parameters?
$template = "Hello, {{name}}!";
$data = [
'name'=> 'world'
];
echo replace($template, $data);
function replace($template, $data) {
$name = $data['name'];
return $template;
}
echo replace($template, $data); must return "Hello, world!"
Thank You!
One way would be to use the built-in str_replace function, like this:
foreach($data as $key => $value) {
$template = str_replace("{{$key}}", $value, $template);
}
return $template;
This loops your data-array and replaces the keys with your values. Another approach would be RegEx
You can do it using preg_replace_callback_array to Perform a regular expression search and replace using callbacks.
This solution is working for multi variables, its parse the entire text, and exchange every indicated variable.
function replacer($source, $arrayWords) {
return preg_replace_callback_array(
[
'/({{([^{}]+)}})/' => function($matches) use ($arrayWords) {
if (isset($arrayWords[$matches[2]])) $returnWord = $arrayWords[$matches[2]];
else $returnWord = $matches[2];
return $returnWord;
},
],
$source);
}
demo here
This question already has answers here:
Using PHP replace regex with regex
(3 answers)
Closed 3 years ago.
I'm trying something like this -
$foo = "foo555bar25foobar1";
if(1 === preg_match('~[0-9]~', $foo, $matches) {
$bar = preg_replace($matches, "-".$matches."-", $foo);
};
echo $bar;
The result I'm looking to achieve is:
foo-555-bar-25-foobar-1-
$foo = "foo555bar25foobar1";
$bar = preg_replace('/\d+/', '-$0-', $foo);
echo $bar;
This question already has answers here:
How to extract only part of string in PHP?
(3 answers)
Closed 5 years ago.
I have a string in a variable like this:
$var = "This is a banana (fruit)";
How do I trim / get the part 'fruit' only? (i.e., data inside braces whatever it is).
Try this:
<?php
$sentence = "This is a banana (fruit)";
$a = stripos($sentence,"(");
$b = stripos($sentence,")");
echo substr($sentence, $a, $b);
?>
Use preg_rpelace function:
<?php
$str = "This is a banana (fruit)";
echo preg_replace('/.*\((.*)\).*/','$1',$str);
?>
This question already has an answer here:
How to decode something beginning with "\u" with PHP
(1 answer)
Closed 8 years ago.
I have one string:
"Hello\u00c2\u00a0World"
I would like convert in:
"Hello World"
I try :
str_replace("\u00c2\u00a0"," ","Hello\u00c2\u00a0World");
or
str_replace("\\u00c2\\u00a0"," ","Hello\u00c2\u00a0World");
but not work!
Resolve!
str_replace(chr(194).chr(160)," ","Hello\u00c2\u00a0World");
If you would like to remove \u.... like patterns then you can use this for example:
$string = preg_replace('/(\\\u....)+/',' ',$input);
You are most of the way there.
$stuff = "Hello\u00c2\u00a0World";
$newstuff = str_replace("\u00c2\u00a0"," ",$stuff);
you need to put the return from str_replace into a variable in order to do something with it later.
This should work
$str="Hello\u00c2\u00a0World";
echo str_replace("\u00c2\u00a0"," ",$str);
You may try this, taken from this answer
function replaceUnicode($str) {
return preg_replace_callback("/\\\\u00([0-9a-f]{2})/", function($m){ return chr(hexdec($m[1])); }, $str);
}
echo replaceUnicode("Hello\u00c2\u00a0World");
This question already has an answer here:
How to unserialize an ArrayObject
(1 answer)
Closed 1 year ago.
I'm trying to parse the 2 values contained within double quotes from a session string. The other string variables are not constant and therefore can not use any additional characters as markers. I only need the quoted values. My following sscanf function is incomplete.
$string = 'a:1:{s:14:"174.29.144.241";s:8:"20110508";}';
sscanf($string,'%[^"]', $login_ip, $login_date);
echo $login_ip;
echo $login_date;
Thanks for your help.
That data is just PHP serialized text from serialize()
In which case you can get at the data you need with:
$sessionData = unserialize('a:1:{s:14:"174.29.144.241";s:8:"20110508";}');
list($ip, $date) = each($sessionData);
$string = 'a:1:{s:14:"174.29.144.241";s:8:"20110508";}';
preg_matches("/(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/",$string,$matches);
echo $matches[1];
This should return your ip address. consult php.net
You can use regex to do that.
$string = 'a:1:{s:14:"174.29.144.241";s:8:"20110508";}';
$pattern = '/"(?P<ip>[^"]*)"[^"]*"(?P<date>[^"]*)"/';
preg_match( $pattern, $string, $matches );
echo $matches['ip'].' '.$matches['date'];
First quoted value will go to ip, second to date.
An interesting hack would be to use explode using " as the separator like this:
<?php
$res = explode('"','a:1:{s:14:"174.29.144.241";s:8:"20110508";}');
print_r($res);
?>
Any value in double quotes would be returned in an odd index i.e $res[1], $res[3]