- Php php variable value in string
- Modify values in variable string with php
- PHP: Evaluating variable in string, and then changing value of variable
- PHP Function and variables stored in a string in a database
- How to put a session variable value into string
- PHP String Interpolation, Using Variables in Strings
- Double Quotes for Interpolation
- Using curly brackets with heredocs
Php php variable value in string
You can add new filters (name -> function name mapping) in . Instead store in database string like this: And when you retrieve it you can just do: or Other common approach is to use .
Modify values in variable string with php
$mystring = "us100ch121jp23uk12";
I) I want to change value of jp by adding +1 so that makes the string into
II) Is there a way to seperate the numeric part and alphabetic part in the above string into:
$string = "us100ch121jp23uk12"; $search_for = "us"; $pairs = explode("[]", $string); // I dont know the parameters. foreach ($pairs as $index=>$pair) < $numbers = explode(',',$pair); if ($numbers[0] == $search_for)< $numbers[1] += 1; // 23 + 1 = 24 $pairs[index] = implode(',',$numbers); //push them back break; >> $new_string = implode('|',$pairs);
using Evan sir’s suggestions
$mystring = "us100ch121jp22uk12"; preg_match_all("/([A-z]+)(\d+)/", $mystring, $output); //echo $output[0][4]; foreach($output[0] as $key=>$value) < // echo "[".$value."]"; echo "[".substr($value, 0, 2).",".substr($value, 2, strlen($value) - 2)."]"."
"; >
If you use preg_match_all(«/([A-z]+)(\d+)/», $string, $output); , it will return an array to $output that contains three arrays. The first array will be country number strings (eg ‘us100’ ). The second will contain country strings (eg ‘us’ ). The third will contain the numbers (eg ‘100’ ).
Since the second and third arrays will have matching indexes ( $output[1][0] will be ‘us’ and $output[2][0] will be ‘100’ ), you could just cycle through those and do whatever you’d like to them.
Here is more information about using regular expressions in PHP. The site also contains information about regular expressions in general, which are a useful tool for any programmer!
You can do it using regular expressions in php. See tutorial: http://w3school.in/w3schools-php-tutorial/php-regular-expression/
Function Description ereg_replace() The ereg_replace() function finds for string specified by pattern and replaces pattern with replacement if found. eregi_replace() The eregi_replace() function works similar to ereg_replace(), except that the search for pattern in string is not case sensitive. preg_replace() The preg_replace() function works similar to ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters. preg_match() The preg_match() function finds string of a pattern and returns true if pattern matches false otherwise. Expression Description 2 It matches any decimal digit from 0 through 9. [a-z] It matches any character from lowercase a through lowercase z. [A-Z] It matches any character from uppercase A through uppercase Z. [a-Z] It matches any character from lowercase a through uppercase Z. p+ It matches any string containing at least one p. p* It matches any string containing zero or more p’s. p? It matches any string containing zero or more p’s. This is just an alternative way to use p*. p It matches any string containing a sequence of N p’s p It matches any string containing a sequence of two or three p’s. p It matches any string containing a sequence of at least two p’s. p$ It matches any string with p at the end of it. ^p It matches any string with p at the beginning of it. [^a-zA-Z] It matches any string not containing any of the characters ranging from a through z and A through Z. p.p It matches any string containing p, followed by any character, in turn followed by another p. ^.$ It matches any string containing exactly two characters. (.*) It matches any string enclosed within and . p(hp)* It matches any string containing a p followed by zero or more instances of the sequence hp.
you also can use JavaScript: http://www.w3schools.com/jsref/jsref_obj_regexp.asp
Modify values in variable string with php, I) I want to change value of jp by adding +1 so that makes the string into. us100ch121jp24uk12 suppose if II) Is there a way to seperate the numeric part and alphabetic part in the above string into: [us , 100] [ch,121] [jp,24] [us,12] my code:
PHP: Evaluating variable in string, and then changing value of variable
I have a string such as the following:
$s1 = "Apples"; $s2 = "$s1 are great"; echo $s2;
My understanding of PHP is that the double-quotes in the second line will cause $s1 to be evaluated within the string, and the output will be
However, what if I wanted to keep $s2 as a «template» where I change $s1’s value and then re-evaluate it to get a new string? Is something like this possible? For example:
$s1 = "Apples"; $s2 = "$s1 are great"; echo $s2 . "\n"; $s1 = "Bananas"; $s3 = "$s2"; echo $s3 . "\n";
I find that the output for the following is:
Apples are great Apples are great
What I am hoping to get is more like:
Apples are great Bananas are great
Is such a thing possible with PHP where I can keep the «template» string the same, change the input variables, and then reevaluate to a new string? Or is this not possible with PHP?
Well, you could do something like this: (just a quick mashup).
$template = " are great"; $s1 = "Apples"; $s2 = "Bananas"; echo str_replace('',$s1,$template) . "\n"; echo str_replace('',$s2,$template) . "\n";
You can try use anonymous function (closure) for similar result: (only above PHP 5.3!)
$s1 = function($a) < return $a . ' are great'; >; echo $s1('Apples'); echo $s1('Bananas');
You can also use reference passing (though it is much more commonly used in functions when you want the original modified):
Apples are great Bananas are great
PHP Function and variables stored in a string in a, PHP Function and variables stored in a string in a database. Ask Question Asked 9 years, 6 months ago. So you need to store in database string with %s as placeholders for values. Example:
Dear %s,
This will allow the user to input any variable to be in the string. My model and related models have many …
PHP Function and variables stored in a string in a database
I need to store data within a database, when I get the data from the database I need functions and variables in the string to be worked out as such.
I then store this in the database, and when I retrieve the string and run it through
then the variable gets populated with the name. This is exactly what I needed and works fine.
The problem is I want to run a function with this variable as the parameter.
example. I would like to ucwords the variable.
$str = "Dear name)>,
" //just echoed name)>, $str = "Dear name)>,
" //Fatal error: Function name must be a string,
Am I going in the right direction? Is this at all possible?
You don’t need to keep PHP code in database. This is a bad practice and also can lead to security vulnerabilities.
Instead store in database string like this:
And when you retrieve it you can just do:
$stringFromDb = str_replace("[name]", $this->name, $stringFromDb);
$stringFromDb = str_replace("[name]", ucwords($this->name), $stringFromDb);
Other common approach is to use sprintf . So you need to store in database string with %s as placeholders for values.
$stringFromDb = sprintf($stringFromDb, ucwords($this->name));
What you seem to be looking for is a simple templating language.
It’s been a long while since I’ve written PHP (and I suddenly remember why. ), but here’s something I whipped up.
It should support both objects ( $a->name ) and arrays ( $a[«name»] ) as input objects.
You can add new filters (name -> function name mapping) in $valid_filters .
$valid_filters = array("title" => "ucfirst", "upper" => "strtoupper"); function _apply_template_helper($match) < global $_apply_template_data, $valid_filters; $var = $match[1]; $filter = $valid_filters[trim($match[2], ':')]; $value = is_array($_apply_template_data) ? $_apply_template_data[$var] : $_apply_template_data->$var; if($filter && !empty($value)) $value = call_user_func($filter, $value); return !empty($value) ? $value : $match[0]; > function apply_template($template, $data) < global $_apply_template_data; $_apply_template_data = $data; $result = preg_replace_callback('/\\>/', "_apply_template_helper", $template); $_apply_template_data = null; return $result; >
$template = "Hello >, you have been selected to win >, >"; echo apply_template($template, array("name"=>"john", "amount" => '$500,000', "salutation" => "congratulations"));
Hello John, you have been selected to win $500,000, CONGRATULATIONS
I have found the following works,
If i contain the function within the class itself then it can be called using the following code
Dear properCase(\$this->rl_account->name)>,
But i would like to be able to do this now without having the database have the code as Alex Amiryan mentions earlier.
PHP — concatenate or directly insert variables in string, You can insert functions semi-indirectly into strings via PHP 5.3 variable functions. $x = function () < >; $string = «hello «, which works around the «restriction». – Marc B Apr 9, 2011 at 16:16 5 @Marc That’s cool, I didn’t know that!$x>
How to put a session variable value into string
How to put session value into this string in PHP
$query = '//Users/User/username[. = "admin"]';
So instead of admin I need to put $_SESSION[‘SESS_FIRST_NAME’];
This should do the trick for you mate
$query = '//Users/User/'.$_SESSION["SESS_FIRST_NAME"];
Assuming the data in the Session is «Joe» this will output:
Try this $query = ‘//Users/User/’.$_SESSION[«SESS_FIRST_NAME»];
$_SESSION[‘SESS_FIRST_NAME’] is just like normal variable so either directly replace it with admin or store its value in a variable
$admin = $_SESSION['SESS_FIRST_NAME']; $query = '//Users/User/username[. = ". $admin . "]';
$query = '//Users/User/username[. = "'.$_SESSION['SESS_FIRST_NAME'].'"]';
Adding PHP string variable to textbox value that, Adding PHP string variable to textbox value that includes quotation marks. I’m trying to populate an HTML text box with a php variable. The variable is a string with a single quotation mark in it and is retrieved from a database. When I echo the variable it looks as it’s supposed to — ie. «here’s my string» so, it’s …
PHP String Interpolation, Using Variables in Strings
How to effectively use variables within strings to insert bits of data where needed.
String interpolation is the process of replacing variables in a string with the content of the variables. This variable interpolation is made possible when the string is either enclosed in double quotes, or when a heredoc is used.
Variables can either be included in the string using a simple syntax, or by surrounding the variables with curly brackets, also known as complex syntax. Using curly brackets allows us to explicitly specify the end of the variable name, which would otherwise risk getting mixed up with the string.
Beginning with the simple syntax:
$some_more_text = 'and some more text'; $my_variable = "This variable contains some text, $some_more_text"; // Output the interpolated variable echo $my_variable;
Note. Only use Double Quotes if the string either contains variables or escape sequences that needs to be interpreted.
The above syntax will not work when we got text surrounding the variable name itself. To solve this, we should use complex curly syntax:
$alphabet_fragment = ', h, i, j, k'; $alphabet = "a, b, c, d, e, f, g$alphabet_fragment>, l, m, n. "; echo $alphabet;
Double Quotes for Interpolation
Generally, it is bad to use the simple syntax for interpolation, since you can easily break the interpolation by accident, and it can also be bad for the readability of your code.
There is a couple of things you can do to increase consistency, and avoid breaking the interpolation:
- Use String Concatenation to combine variables with your strings.
- Use Curly Brackets syntax for interpolation.
Personally, I prefer to use concatenation for interpolation on small strings, and Curly Brackets when using heredocs for loading template files or working with larger strings.
$alphabet_fragment = ', h, i, j, k'; $alphabet = 'a, b, c, d, e, f, g' . $alphabet_fragment . ', l, m, n. ';
Using curly brackets with heredocs
When you are using the heredoc syntax, you will most commonly be working with large strings. One scenario where this is useful, is when keeping a HTML template in a PHP variable.
However, heredocs also increases the chance of variable-text mixups, which makes the complex syntax particularly useful in heredocs:
$template = _LOADTEMPLATE _LOADTEMPLATE; // End of the heredoc