This question already has answers here:
PHP: Escape illegal chars in .ini-files
(2 answers)
Closed 1 year ago.
When using parse_ini_file, it will attempt to read and understand the ini file. I'm looking for a solution that will read the file, place it into arrays much like parse_ini_file does, however I don't want it to get to the special characters in my ini file and spit an error that it can't parse them. Could anyone point me in the right direction?
ini file for reference:
[IMPORT]
email=email#thisisanemail.com
location=
Description=Order Form
name=*.xls*
matrixfile=
matrix_field=
matrix_disc_type_column=
matrix_disc_percentage_column=
fixed=XLS
separator=|
RowTerminator=
headerrows=13
footerrows=0
maxexcelcolumns=7
If I parse this, it gets stuck on the seperator "|" but I need that there.
If you use parse_ini_file with the scanner_mode parameter to specify INI_SCANNER_RAW, then option values will not be parsed (see
https://www.php.net/manual/en/function.parse-ini-file.php).
Related
This question already has answers here:
How do I escape an exclamation mark in bash?
(4 answers)
Closed 2 years ago.
I want to pass a string to a php script which contains ! character. Like this
php cfg.php --name=smtppass --set="MYW!ORD"
But I get this error
bash: !ORD: event not found
On the other hand, if I pass "MYW\!ORD", I see this string is set when I query the file
smtppass MYW\!ORD
Any idea to fix that?
You could pass it like this:
php cfg.php --name=smtppass --set="MYW"'!'"ORD"
or a more lazy way would be to use single quotes.
php cfg.php --name=smtppass --set='MYW!ORD'
This happens because of Bash's history expansion, which is sometimes very dangerous, you could turn this feature off by typing set +H.
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 4 years ago.
I have an XML file that's read using PHP's file_get_contents so other changes can be done to it.
I need to find and remove the nodes <BATCHALLOCATIONS.LIST>...<BATCHALLOCATIONS.LIST> (not just those two lines, but what's between the entire node) in the entire file.
Since the file is already loaded using file_get_contents I'd like to do this without having to load the file again using simpleXML, or an XML parser or any other method (like DOM).
The node does not have a specific parent and appears randomly.
The XML file is exported from a Business Accounting Software.
Any idea on how to achieve this? Maybe using a Regular Expression to do a search and replace or something like that?
I've been trying to do this using a regular expression and preg_replace, but just can't get things to work.
Here's just a portion of the file. The original runs to 10K+ lines.
This should have worked but doesn't
preg_replace('/^\<BATCHALLOCATIONS.LIST\>(.*?)\<\BATCHALLOCATIONS.LIST\>$/ism','', $newXML);
I'm trying to do this without using any HTML/XML parser.
There's probably a better way to do it, but this will work
// get your file as a string
$yourXML = file_get_contents($file) ;
$posStart = stripos($yourXML,'<BATCHALLOCATIONS.LIST>') ;
$posEnd = stripos($yourXML,'</BATCHALLOCATIONS.LIST>') + strlen('</BATCHALLOCATIONS.LIST>') ;
$newXML = substr($yourXML,0,$posStart) . substr($yourXML,$posEnd) ;
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:
Remove a child with a specific attribute, in SimpleXML for PHP
(18 answers)
Closed 7 years ago.
I know there are a lot of answers out there for this exact question, but none of them seem to help me solve my problem.
I have an xml file on my server, i need to use PHP SimpleXML to remove an element from the document. After some googling i found a number of answers saying to use unset() and then save the xml.
so i came up with this:
function deleteCourse($course){
$xml = self::getxml(); # get the XML file
unset($xml->xpath('course[name = "'.$course.'"]'));
$xml->asXml("data.xml");
}
now whenever i run this i get this error: PHP Fatal error: Can't use method return value in write context in blahblahLink on line 92
line 92 is unset($xml->xpath('course[name = "'.$course.'"]'));
I really hope somebody can help me out with this
unset won't work if you pass method return, pass variable content / array instead
This question already has answers here:
Can I echo a variable with single quotes?
(11 answers)
Closed 9 years ago.
Im having some troubles unlinking files from a directory. I'll try to explain it as good as i can.
Im trying to delete a file from a directory, first i get the file name from my database, where is stored in field "avatar".
these are my involved vars:
$avatar1=mysqli_query($con,"Select avatar from users where user='$_SESSION[Username]'");
$avatar2=mysqli_fetch_array($avatar1);
$avatardirectory = $avatar2['avatar']; //(missunderstanding name, its actually a file).
So far, when i print $avatardirectory when my user is hodor, it shows "hodor.png"
Ok, here comes the nasty part, i try to go with this:
unlink('/var/www/html/test/img-gallery/$avatardirectory'); //This wont work.
Then I just do the same exact thing but with filename:
unlink('/var/www/html/prueba/img-gallery/hodor.png'); //This actually works.
And now I'm totally lost.
Variables arn't expanded inside single quote strings.
You might want to see the difference in
echo '/var/www/html/test/img-gallery/$avatardirectory'
and
echo "/var/www/html/test/img-gallery/$avatardirectory"
Note that unlink() removes files, not directories. use rmdir() to remove (an empty) directory. (though it sounds like your $avatardirectory holds the name of a file, not a directory(?) )