How can I add variables and quotes to a variable?
in the output it just prints the variables
This is the code I have tried
$pl2 = '{"comment":"' . $nmp3 . '","file":"' . $pmp3 . '"},';
Try with:
$pl2 = json_encode(array(
'comment' => $nmp3,
'file' => $pmp3
));
Try this, it should work:
$p = ' {"comment": ' . $nmp3;
$p = $p.' "," file " : " ' . $pmp3;
$p=$p.' "}," ';
echo $p;
Related
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?>";
I want to store an property->object name in a table and access in code.
What is the proper way to accomplish the following.
$patient = new stdClass();
$patient->patient_number = '12345';
$cls = 'patient';
$prp = 'patient_number';
echo 'test1 = ' . $patient->patient_number . '<br>';
//--- this prints 12345 as expected;
echo 'test2 = ' . $$cls->patient_number . '<br>';
//--- this print 12345 as expected;
echo 'test3 = ' . $$cls->${$prp} . '<br>';
//--- this generates undefined variable patient_number;
Use this:
echo 'test3 = ' . ${$cls}->{$prp} . '<br>';
I'm trying to use Simple_Html_Dom parser to parse a web page for NBA statistics. I'm going to have a lot of different urls, but parsing the same data, so I figured I would create a function. Outside of the function, this parser works great, however as soon as I place the parsing inside the function, I receive a connection error. Just wondering, if anyone knows why I can't run the file_get_html inside the function. Here is the code below. Please help!
include('simple_html_dom.php');
$nbaPlayers = 'playerstats/15/1/eff/1-2';
function nbaStats($url){
$html = 'http://www.hoopsstats.com/basketball/fantasy/nba/';
$getHtml = $html . $url;
$a = file_get_html("$getHtml");
foreach($a->find('.statscontent tbody tr') as $tr){
$nbaStatLine = $tr->find('td');
$nbaName = $nbaStatLine[1]->plaintext;
$nbaGamesPlayed = $nbaStatLine[2]->plaintext;
$nbaMinuesPlayed = $nbaStatLine[3]->plaintext;
$nbaTotalPoints = $nbaStatLine[4]->plaintext;
$nbaRebounds = $nbaStatLine[5]->plaintext;
$nbaAssists = $nbaStatLine[6]->plaintext;
$nbaSteals = $nbaStatLine[7]->plaintext;
$nbaBlocks = $nbaStatLine[8]->plaintext;
$nbaTurnovers = $nbaStatLine[9]->plaintext;
$nbaoRebounds = $nbaStatLine[11];
$nbadRebounds = $nbaStatLine[12];
$nbaFieldGoals = $nbaStatLine[13];
$nbaFieldGoals = explode("-", $nbaFieldGoals);
$nbaFieldGoalsMade = $nbaFieldGoals[0];
$nbaFieldGoalsAttempted = $nbaFieldGoals[1];
// Player Stat Line
$playerStats = $nbaName . ': gp - ' . $nbaGamesPlayed . ' mp - ' . $nbaMinutesPlayed . ' pts - ' . $nbaTotalPoints . ' rb - ' . $nbaRebounds . ' as - ' . $nbaAssists . ' s - ' . $nbaSteals . ' bl - ' . $nbaBlocks . ' to - ' . $nbaTurnovers;
echo $playerStats . '<br /><br />';
}
}
nbaStats($nbaPlayers);
When passing a variable to a function you don't need to wrap the variable in quotations. Quotations are used when passing text directly to a function or assigning text to a variable.
Change this:
$a = file_get_html("$getHtml");
To this:
$a = file_get_html($getHtml);
From LDAP I'm querying my users and this code sets them as a variable in the quoted format I need to run the MySQL query which would be 'username','other_username', etc...
foreach ($prefs as $who => $pref) {
if (strpos($who, 'public') === false) {
$team_users_string .='\'' . $who . '\',';
}
When I try to sanitize the command with the following code it converts the string to \'username\',\'other_username\', what can I do to correct this?
$team_users = rtrim($team_users_string, ",");
$start_date = $_POST['start_year'] . '-' . $_POST['start_month'];
$end_date = $_POST['end_year'] . '-' . $_POST['end_month'];
echo 'Welcome, <strong>' . $user . '</strong><br />';
echo '<br />';
echo '<strong>Selected Start Date:</strong> ' . $start_date . '<br />';
echo '<strong>Selected End Date:</strong> ' . $end_date . '<br />';
mysql_real_escape_string($team_users),
mysql_real_escape_string($start_date),
mysql_real_escape_String($end_date));
$query = "SELECT * FROM vacation WHERE user_name in ($team_users) AND day BETWEEN '$start_date-01' AND '$end_date-31'";
Your problem is that you're adding the quote characters before you pass the string to mysql_real_escape_string(). So the literal quotes become escaped by that function.
You could avoid this by using mysql_real_escape_string(), and then delimiting the result in quotes.
Also I'd use an array and implode() the array to get commas, instead of being forced to rtrim() the last comma.
foreach ($prefs as $who => $pref) {
if (strpos($who, 'public') === false) {
$team_users_array[] = "'" . mysql_real_escape_string($who) . "'";
}
}
$team_users = implode(",", $team_users_array); // no rtrim needed
$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)