This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
for a localized website I want to create different language files.
But my main problem before starting the localiuation is, that i probably have strings with variables.
My theory is that i can use placeholders within my language files like:
$lang['somekey'] = "Hello Mr. %s, how are you?";
Is there a clean and nice way to parse those variables or do i have to develop a function for that?
Thanks.
I have the same problem and do it simply by using,
echo sprintf($this->lang->line('somekey'),'XYZ');//load language library before use
Read sprintf()
you can use codeigniter i18n with PHP .sprintf() to achieve what you want. load up the codeigniter non-variable strings (with those format stuff), then pass it on to .sprintf() for formatting and assignment of values. it should replace the %s part.
it's similar to this question. .sprintf() works like .printf(), only that it returns the string rather than printing it.
Related
This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you
This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page
This question already has answers here:
What is the difference between a language construct and a "built-in" function in PHP?
(4 answers)
Closed 8 years ago.
PHP has a large number of batteries-included functions, e.g. functions on arrays. Some of these, like each, are present in get_defined_functions()['internal']. Others, like reset and many others, are not present at all. However, they are treated as functions in every other way: they are not documented as "language constructs" or keywords; I can call them using the "variable function" feature; function_exists("reset") returns true; if I try to redefine them (e.g. function reset() { ... }), I get an error about redeclaration, rather than a syntax error; and so on.
Why are these functions not listed by get_defined_functions? Are they not actually functions? If not, what are they? If they are functions, then what actually is it that get_defined_functions is listing? In either case, how do I list the things that don't appear in get_defined_functions?
Quite a short answer: Reset is present in get_defined_functions()['internal'].
Look at [1532] in this fiddle: http://phpfiddle.org/main/code/h5n-ndx
This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I can do this:
home
But when I have a link with an Id, I can't pass variables:
home <--!this doesn't work of course -->
Thanks a lot!
When using PHP you can not use '#' in the URL, it will not be passed to the server.
You can use urlencode in order to encode the non-alphanumeric characters.
Use window.location.hash in javascript
<script>alert(window.location.hash);</script>
And the parse_url() function in PHP
<?php echo parse_url("home.php?var=home#sectionID",PHP_URL_FRAGMENT);?>
your HTML will be..
home
Declare a variable with $home dollar sign, i think you getting confused with JavaScript variable and PHP. To retrieve the variable values on a new page use $_GET['ID'].
Hope this helps.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Problem when loading php file into variable (Load result of php code instead of the code as a string)
I am trying to create a template based application and I am having issues writing the user data into a pre-existing template file.
I would initially use file_get_contents('template.php');
and template.php would contain the following:
echo $user;
Is there anyway to insert data into a placeholder variable in a template file using file_get_contents?
I suppose I could use DomDocument or regex to insert the data into a placeholder string, but would this be possible to do with a php variable?
If you have control over template.php and are aware of the security considerations, you're probably looking for include or require:
include('template.php');
Will do what you want to do.
EDIT
Output buffering
ob_start();
include('template.php');
$result = ob_get_clean();