I'm trying to take a variable from an xml file and save it as a php file
$xmlresponse = simplexml_load_string($dom->saveXML());
$shipmentid = $xmlresponse->ListInboundShipmentsResult->ShipmentData->member->ShipmentId;
echo '<br />' . $shipmentid . '<br />';
$todaydt = new DateTime('today');
$today3 = $todaydt->format('m-d-Y') . PHP_EOL;
$today = rtrim($today3);
$var_str = var_export($shipmentid, true);
$var = "<?php\n\n\$shipmentid = $var_str;\n\n?>";
file_put_contents('shipmentid_' . $today . '.php', $var);
}
But in my shipmentid_11-11-2016.php file it is giving me
$shipmentid = SimpleXMLElement::__set_state(array(
0 => 'shipmentid',
));
How can I save the variable so it is just simply
$shipmentid = 'shipmentid';
?
When you do
echo '<br />' . $shipmentid . '<br />';
php implicitly converts $shipmentid value to a string. var_export does not do it, so you need to convert it by yourself:
$var_str = strval($shipmentid);
Next thing is that $var_str is a string and does not contains any quotes around. So, again you need to write quotes by yourself:
// with double quotes
$var = "<?php\n\n\$shipmentid = \"$var_str\";\n\n?>";
// with single quotes
$var = "<?php\n\n\$shipmentid = '$var_str';\n\n?>";
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
I'm trying to add new line at the end of string, so I could better see <option> in new lines, hovewer I get string with "\r\n" as text instead of new lines. What is wrong with code ?
foreach ($xml['ROW'] as $ar) {
$tekstas = $ar['zz'] . ' (' . $ar['xx'] . ')';
$insert .= '<option value="' . $ar['Adresas'] . '">' . $tekstas .'</option> "\r\n"' ;
}
echo nl2br(htmlentities($insert));
Almost there, look at the right way to concatenate the new line to the rest of the string you generate.
foreach ($xml['ROW'] as $ar) {
$tekstas = $ar['zz'] . ' (' . $ar['xx'] . ')';
$insert .= '<option value="' . $ar['Adresas'] . '">' . $tekstas .'</option> ' . PHP_EOL ;
}
echo nl2br(htmlentities($insert));
'-quoted strings do not honor escaped metacharacters like "-quoted ones do:
echo '\r' - outputs a literal backslash and an 'r'
echo "\r" - outputs a carriage return
the only escapes that are supported in ' strings are \' and \\.
So..
'</option> "\r\n"'
^---open single quote string
^---close single-quote string
when it should be
'</option> ' . "\r\n"
instead.
"\r\n" should be in double quotes.
foreach ($xml['ROW'] as $ar) {
$tekstas = $ar['zz'] . ' (' . $ar['xx'] . ')';
$insert .= '<option value="' . $ar['Adresas'] . '">' . $tekstas .'</option>'."\r\n";
}
echo nl2br(htmlentities($insert));
Basically, I have a variable that is built from a multidimensional array via a foreach loop.
I need to somehow validate the format that it is in, although I am not to sure how to go about this since I am still quite a newbie when it comes to PHP.
This is how my string will look:
question1,answer1,answer2,answer3,answer4
question2,answer1,answer2,answer3,answer4
question3,answer1,answer2,answer3,answer4
I need to validate that exact format, being:
string[comma]string[comma]string[comma]string[comma]string
string[comma]string[comma]string[comma]string[comma]string
string[comma]string[comma]string[comma]string[comma]string
I need to validate it with a boolean variable if possible, to return true or false if it matches the format or not. These strings can contain anything such as letters, numbers, special characters, etc.
Each line will ALWAYS have 5 strings and 4 commas, never more and never less.
If needed, this is the code that builds the multidimensional array and then converts it into a string. It is grabbing an uploaded CSV file, transferring it into a multidimensional array, and then building the string afterwards. As you can see, my array is starting at 1 instead of 0, no point explaining why since that is off topic.
$csv_array = array(array());
if (!empty($_FILES['upload_csv']['tmp_name']))
{
$file = fopen($_FILES['upload_csv']['tmp_name'], 'r');
}
if($file)
{
while (($line = fgetcsv($file)) !== FALSE)
{
$csv_array[] = array_combine(range(1, count($line)), array_values($line));
}
fclose($file);
}
if(!empty($csv_array[1]))
{
foreach (array_slice($csv_array,1) as $row)
{
$all_questions = $all_questions . $row[1] . ",";
$all_questions = $all_questions . $row[2] . ",";
$all_questions = $all_questions . $row[3] . ",";
$all_questions = $all_questions . $row[4] . ",";
$all_questions = $all_questions . $row[5];
$all_questions = $all_questions . "\n";
}
}
All help is greatly appreciated.
Thanks in advance!
Sounds like a regex could do the job:
$match_ok = preg_match('/^([^,]+,){4}[^,]+$/', $test_string);
If you want to allow [string] to be empty, you may change all + to *.
Explanation: The first ^ matches the line start, then one or more characters, that are not commas, are expected followed by a comma. This sequence has to be there 4 times and has to be followed by another string not terminated by a comma. The $ matches the end of the line.
If you want to match the while multiline string, you may use:
$match_ok = preg_match('/^(([^,]+,){4}[^,]+\n){3}$/', $test_string);
(since preg_match works in multiline mode by default)
Edit 2: You may even make the regex match to a string not ending with a newline by handling the last line separately, just as it is done with the cols:
$match_ok = preg_match('/^(([^,]+,){4}[^,]+\n){2}([^,]+,){4}[^,]+$/', $test_string);
Try This simple one, May be so many other solutions possible for this
<?php
$test_string = "question1,answer1,answer2,answer3,answer4";
$newarray=explode(',',$test_string);
if(count($newarray)=='5'){
your code......;
}
?>
--------------------TRUE FALSE-------------
<?php
$test_string = "questionAsdAD1###,234234,answer2,answer3,answer4";
function ToCheckMyStrung($test_string){
$newarray=explode(',',$test_string);
if(count($newarray)=='5'){
return true;
} else {
return false;
}
}
?>
in foreach
use
implode(',',$row); for proper formatting.
You can do it while you are creating your string variable,
$count=1;
$trace=array();
if(!empty($csv_array[1]))
{
foreach (array_slice($csv_array,1) as $row)
{
$all_questions = $all_questions . $row[1] . ",";
$all_questions = $all_questions . $row[2] . ",";
$all_questions = $all_questions . $row[3] . ",";
$all_questions = $all_questions . $row[4] . ",";
$all_questions = $all_questions . $row[5];
$all_questions = $all_questions . "\n";
if($row[1]=="" || $row[2]=="" || $row[3]=="" || $row[4]=="" || $row[5]=="")
{
$trace[]=$count;
}
$count++;
}
}
and than just make use of $trace array whenever you want.
A regular expression will help you out:
$test_string = "Some,string,you,want,to,test";
if(preg_match('#^([a-zA-Z0-9]+\,)+([a-zA-Z0-9])+$#', $test_string)) {
echo 'All ok';
}
$brand_condition = ' AND ' . mysql_real_escape_string($brand_selection) . ' IN ';
$brand_condition .= $quote10 . '"'. mysql_real_escape_string($brand_value) . '"' .$quote9;
$brand_conditions[] = $brand_condition;
$query .= implode(' AND ', $brand_conditions) . '';
This produces: AND manufacturer IN ("brand1,brand2")
Since I'm using the IN statement, I need the values to be quoted. At the same time, I am escaping potential quotes with mysql_real_escape_string.
Does anyone see a simple way to get around this small problem?
function quote_escape(&$str) {
$str = '"' . mysql_real_escape_string(chop($str)) . '"';
}
$brands = explode(',', $brand_value);
array_walk($brands, "quote_escape");
$brands = implode(',', $brands);
or
function quote_escape($str) {
return '"' . mysql_real_escape_string(chop($str)) . '"';
}
$brands = implode(',', array_map("quote_escape", explode(',', $brand_value)));
How about $brand_conditions[] = '"'.$brand_condition.'"'; so your adding quotes right before you add the brand_condition in your array.
$concurrent_names = array("O'reilly", 'Tupac "MC New York" Shakur', 'Nemoden');
$escaped_concurrent_names = array_map('mysql_real_escape_string', $concurrent_names);
$condition = 'WHERE name in ("'.implode('", "', $escaped_concurrent_names).'")';
Use this to add quotes for imploded string.
$values = implode( " ',' ", array_values($values) );
$brands=array(nokia,samsung,xiomi);
$brands=implode(" ',' ",$brand);
//$brands='nokia','samsung','xiomi';
WHERE column_name IN ($brands)
My query:
mysql::getInstance()->update('requests', array('response' => mysql_real_escape_string($_POST['status'])), array('secret' => $_POST['secret'])); ?>
If i wand to add string with "&" symbol, all symbols after "&" stripped.
Example:
string: !"№;%:?()_+!##$%^&()_+
in database i see only: !"№;%:?*()_+!##$%^
How to fix this?
update function, if anyone need:
function update($table, $updateList, $whereConditions)
{
$updateQuery = '';
foreach ($updateList as $key => $newValue) {
if (!is_numeric($newValue)) {
$newValue = "'" . $newValue . "'";
}
if (strlen($updateQuery) == 0) {
$updateQuery .= '`' . $key . '` = ' . $newValue;
} else {
$updateQuery .= ', `' . $key . '` = ' . $newValue;
}
}
return $this->query('UPDATE ' . $table . ' SET ' . $updateQuery . $this->buildWhereClause($whereConditions));
}
UPD:
echo mysql::getInstance()->getLastSQL() says:
UPDATE requests SET `response` = '!\"№;%:?*()_ !##$%^' WHERE `secret` = '52a4ab5f7f3e8491e60b71db7d775ee2'
so, problem with function update in mysql class?
Slaks, i need to use str_replace('&', '%28', $query); ?
You're probably passing a raw & character in the querystring, which causes everything after the & to be parsed as second parameter by PHP. (Before it gets into your variable)
You need to escape the & as %26.
EDIT: You need to escape it before you send it to the server. (When you make the HTTP request)