This question already has answers here:
PHP what is the best way to write data to middle of file without rewriting file
(3 answers)
How to insert some text in middle of file in PHP [closed]
(4 answers)
Closed 4 years ago.
I am generating some PHP source code files using PHP only :)
And I want to know if I have to write some line of text before closing brackets, how can I do that using PHP file functions.
e.g. below is my target file code. filename : project.php
Class project{
public function get_project($id){
..... some code here
}
public function add_project(){
..... some code here
}
<--- add code here
}
I have a line of code which I want to add to the position pointed in the example. I can use file_put_content() method but it has option to Append the code on the end of the file like below
file_put_contents('project.php', 'Hello World', FILE_APPEND);
but it will not write to specific position. So how can I do that?
Related
This question already has answers here:
Can I create a PHP function that I can call without parentheses?
(6 answers)
Closed 6 years ago.
all php functions need () in the end. However, exit doesnt need that.
Can I create a function manually, which I can later execute without () ?
Even more, If I have full access to php installation?
p.s. please dont tell me answers "exit is not function" or etc (My question is not if "exit" is function or not). I want to know HOW TO ACHIEVE like that.
No you can't. You have to edit Base of PHP language to accomplish this.
exit , echo , print and etc are not function .
This question already has answers here:
Create or write/append in text file
(7 answers)
Closed 6 years ago.
I am able to take the values from a URL's query string and store them in some variables, however I would also like to print or write these values to a single line in a .txt document, and save that document in a given directory on the server.
Each time the page is loaded with a new query string, a new line will be added to that .txt file with the string values, and the file re-saved.
Can this be done? Struggling to find answers to this in my searches.
Many thanks in advance.
As simple as:
$url_value = "This is some value\n";
file_put_contents("/your/filename/here.txt", $url_value, FILE_APPEND );
See file_put_contents() for more information.
This question already has answers here:
Unable to add namespace to an attribute with PHP's SimpleXML
(3 answers)
Closed 8 years ago.
I am very new to XML so apologies if this is a simple question;
I have the following line of code in a PHP file which is used to create a resulting line of code in an XML file via a PHP form;
$Savings = $xml->createElement('Savings');
This creates the following line in my XML file;
<Savings>263.4</Savings>
How would I change this line in my XML file to look as follows;
<Savings Currency="EUR">263.4</Savings>
I have tried the following but I get no additional output;
$Savings = $xml->createElementNS('EUR', 'Savings');
Use setAttribute:
$Savings = $xml->createElement('Savings');
$Savings->setAttribute('Currency', 'EUR');
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();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find out where a function is defined?
in a client's website I have a php code:
return userHasActivePurchase($iId);
The website is full of icludes, requires.
I have made a full text search on "userHasActivePurchase" to find out what php file does contain this function without success.
My question is : I have a function name and I would like to know what php files contains that function.
I tried with:
print_r(userHasActivePurchase);
without success.
Please help !
As per this stackoverflow thread:
$reflFunc = new ReflectionFunction('function_name');
echo $reflFunc->getFileName() . ':' . $reflFunc->getStartLine();
You can search for text "function userHasActivePurchase" in the whole code base using grep command or by any editor's native search in directory function.
If you find this function defined at more than one place you can recognize it by putting a statement inside the function in all the files
echo __FILE__
get a decent ide (phpstorm, eclipse pdt) which will allow you to follow code - or even worst case allow you to search an entire project for a given term.