I run 2 applications on my site and want to use that same template for both. My Joomla site stores it's template config in a params.ini file in the following manner:
sidebara_width=150
sidebarb_width=300
mainbody_width=500
each parameter in 1 line no commas or semicolon after that. I want to use the same values for my other template. like <div id="sidebars" style="<?php echo $sidebara_width ?>.px">
I need a small php script which can read these values from the params.ini file and assign a value of 150 to a variable called $sidebara_width, assign a value of 300 to a variable called $sidebarb_width and so on.
Kindly help
there is a function called
parse_ini_file
you could use it like this
$ini_array = parse_ini_file('path to file');
$sidebara_width = $ini_array['sidebara_width']
$sidebarb_width = $ini_array['sidebarb_width']
and so on
regards
You might want to checkout the parse_ini_file (http://php.net/manual/en/function.parse-ini-file.php)
Otherwise you can use preg_match_all (http://ca2.php.net/preg_match_all) and use a simple RegEx to match it.
Christian
Related
I have built a custom CMS. Recently, I added the ability to create pages dynamically. I used CKEditor for the content of these pages.
I would also like to run some php functions that may be included in the content of the page stored in mysql.
I DO NOT want to store actual PHP code in the database, but rather function names perhaps. For example, in a page stored in the database I may have.
<?php //begin output
hello world!
check out this latest news article.
news($type, $id);
//end output
?>
What is the best way to find and execute this existing function without using EVAL if its found in the output? I was thinking along the lines of wordpress style short codes. Maybe [[news(latest, 71]] ? Then have a function to find and execute these functions if they exist in my functions.php file. Not really sure the best way to go about this.
I'm not searching for any code answers, but more of a best practice for this type of scenario, especially one that is safest against possible injections.
I found a solution from digging around and finding this thread
How to create a Wordpress shortcode-style function in PHP
I am able to pass short codes like this in CKEditor
[[utube 1 video_id]]
Then, in my page that renders the code:
print shortcodify($page_content);
using this function:
function shortcodify($string){
return preg_replace_callback('#\[\[(.*?)\]\]#', function ($matches) {
$whitespace_explode = explode(" ", $matches[1]);
$fnName = array_shift($whitespace_explode);
return function_exists($fnName) ? call_user_func_array($fnName,$whitespace_explode) : $matches[0];
}, $string);
}
If the function name exist (utube) it will fire the function.
Only problem Im having at the moment is not matter where I place the [[shortcode]] in my editor, it always executes first.
For example, in CKEditor I put:
Hello world! Check out my latest video
[[utube 1 video_id]]
It will always put the text under the video instead of where it is in the document. I need to figure a way to have the short code execute in the order it is placed.
I'm trying to figure out how to add to a PHP data array externally using PHP.
Say if this array, below. Was in a file called Index.php
$data=array("user1"=>array("url"=>"user1.pdf","password"=>"pass1"),
"user2"=>array("url"=>"user2.php","password"=>"pass2"));
and I wanted to add third user using a different Php file taking inputs from somewhere else to insert into the url, password and user namespace.
Thanks.
$data['user3']=array("url"=>"user3.pdf","password"=>"pass3")
I believe include_once() or require_once() should do the trick.
include_once('index.php');
array_push($data,"user3"=>array("url"=>"user3.pdf","pass"=>"123"));
include_once or require_once functions are similar to executing that file once and continuing further. http://in1.php.net/include_once
Alternatively, php now allows for object-oriented programming, so if you are familiar with it you can take a shot at that
If you need it from different file, then the simplest way is to include the other file, which adds users to $data.
index.php
$data = array(
"user1"=>array("url"=>"user1.pdf","password"=>"pass1"),
"user2"=>array("url"=>"user2.php","password"=>"pass2")
);
other_file.php
include "other_file.php";
$data["user3"] = array("url"=>"user3.php","password"=>"pass3");
// or
array_push($data, array("user3" => array("url"=>"user3.php","password"=>"pass3"));
Are you sure you need that other file?
I am a newbie coder trying to build a simple web app using PHP. I am trying to send an HTML email that has a variable that will change each time it is sent. The code to initiate the email is 'email.php' and contains:
$body = file_get_contents('welcome/green2.html.php');
Within the 'green2.html.php' file, I have a variable called $highlight that needs to be populated. The $highlight variable is defined within the 'email.php' file. I had tried to simply add within the 'green2.html.php' file, however it is not being parsed. I get a blank space where the variable should be when it is output.
Also, I have done an include 'welcome/green2.html.php' within the 'email.php' file. When I echo it, the $highlight var is shown on the resulting page, but not if I echo $body.
Any help would be much appreciated!
Have you tried the str_replace function? http://php.net/manual/en/function.str-replace.php.
Add a placeholder in HTML (for instance #name# for name, #email# for email), and then use the string replace function once you've loaded the content of the file.
$bodytag = str_replace("#name#", $name, $myfile);
Loading a file via file_get_contents() will not cause it to be parsed by PHP. It will simply be loaded as a static file, regardless of whether it contains PHP code or not.
If you want it to be parsed by PHP, you would need to include or require it.
But it sounds like you're trying to write a templating system for your emails. If this is what you're doing, you'd be better off not having it as PHP code to be parsed, but rather having placeholder markers in it, and then using str_replace() or similar functions to inject variables from your main program into the string.
Hope that helps.
Use http://php.net/manual/en/function.sprintf.php put a %s in your code instead of the variable read the content and put the string into the sprintf with the variable you want to put that's it. Hope this will help.
I have a page ("main.php") which loads content from an external PHP file ("rpc.php"). Using the below syntax on main.php successfully pulls in content from rpc.php:
$("#portfolioContent").load("rpc.php?o="+day+"");
On rpc.php I have an if statement (part of a long switch function), as follows:
if ( $pagename == "home" ) {
break;
}
This break is not occuring because the variable has not been set. rpc.php is used by various parent pages so the variables need to be set on those. On a parent page I have tried using the following code to attempt to set the variable and pass it to rpc.php but to no avail:
$("#portfolioContent").load("rpc.php?o="+day+"$pagename="home"");
Can anybody point me in the right direction? Thank you.
It should be like this,
$("#portfolioContent").load("rpc.php?o="+day+"&pagename=home");
Your syntax is wrong. Try this:
$("#portfolioContent").load("rpc.php?o="+day+"&pagename=home");
Notice i substituted the $ with a &
Cheers
change this line
$("#portfolioContent").load("rpc.php?o="+day+"$pagename="home"");
to this
$("#portfolioContent").load("rpc.php?o="+day+"pagename="home");
then access the variable with $_GET['pagename']
To access variables from the URL in PHP just do:
$_GET['variablename']
For example, with the URL http://www.example.com?hello=hellWorld
echo $_GET['hello'];
would print helloWorld
Also, you use & to separate variables, not $
I have a dedicated server that I use to crunch lots of data. The way I have it now, I can open a script with a process ID like example.php?ex_pid=123 and just let it go. It downloads a small portion of data, processes it, then uploads it into a database then starts again.
Ideally, I would like to call example.php?ex_pid=123 directly and not by passing a variable to example.php like exec('./example.php'.' '.EscapeShellArg($variable)); to keep it from acting globally.
I don't care about the output, if it could execute in the background, that would be brilliant. The server is an Ubuntu distribution btw.
Is this even possible? If so, any help and examples would be more then appreciated.
You could do something like:
exec("./example.php '".addslashes(serialize($_GET))."');
And then in example.php do something like this:
count($_GET) == 0 && $_GET = unserialize(stripslashes($_SERVER['argv'][1]))
The main issue with that is that ?ex_pid is GET data which is generally associated with either including the file or accessing it through a browser. If you were including the file or accessing it from a web browser this would be trivial, but running it as CLI, your only option would be to pass it as an argument, unfortunately. You can pass it as ex_pid=123 and just parse that data, but it would still need to be passed as an argument but doing that you could use parse_str() to parse it.
Depending on what the script does, you could call lynx to call the actual page with the get data attached and generate a hash for an apikey required to make it run. Not sure if that is an option, but it is another way to do it how you want.
Hope that helps!
I had a real problem with this and couldn't get it to work running something like example.php?variable=1.
I could however get an individual file to run using the exec command, without the ?variable=1 at the end.
What I decided to do was dynamically change the contents of a template file , depending on the variables I wanted to send. This file is called template.php and contains all the code you would normally run as a $_GET. Instead of using $_GET, set the value of the variable right at the top. This line of code is then searched and replaced with any value you choose.
I then saved this new file and ran that instead.
In the following example I needed to change an SQL query - the template file has the line $sql="ENTER SQL CODE HERE";. I also needed to change the value of a a variable at the top.
The line in template.php is $myvar=999999; The code below changes these line in template.php to the new values.
//Get the base file to modify - template.php
$contents=file_get_contents("template.php");
$sql="SELECT * FROM mytable WHERE foo='".$bar."'";
$contents=str_replace("ENTER SQL CODE HERE",$sql,$contents);
//Another search
$contents=str_replace("999999",$bar,$contents);
$filename="run_standalone_code".$bar.".php";
//If the file doesnt't exist, create it
if(!file_exists($filename)){
file_put_contents($filename, $contents);
}
//Now run this file
$cmd="/usr/local/bin/php ".$filename." >/dev/null &";
exec($cmd);
I had completely forgotten about this question until #Andrew Waugh commented on it (and I got an email reminder).
Anyways, this question stemmed from a misunderstanding as to how the $argv array is communicated to the script when using CLI. You can pretty much use as many arguments as you need. The way I accomplish this now is like:
if (isset($argv)) {
switch ($argv[1]) {
case "a_distinguishing_name_goes_here":
$pid = $argv[2];
sample_function($pid);
break;
case "another_name_goes_here":
do_something_else($argv[2]);
break;
}
}