PHP implode array issues - php

Newbie technical difficulty here.
Hope I can explain this clearly.
in My DB, I have snFreq, snFreq2, snFreqIV, snFreqTube, snFreqTrach, snFreqCath, snFreqWound, and snFreqOstomy.
I wrote an isset to check each column if data exist, then make a variable with preset text.
After all the checkpoints, I wanted to include this in a phrase, but don't want to include the "0" data. For some reason my output is still showing multiple commas with blank data.
Here's an example output:
SN FREQUENCY/DURATION: QD X 60 DAYS + 2 PRN VISITS FOR IV COMPLICATIONS, TUBE FEEDING COMPLICATIONS, , , , < = with extra commas?
In my DB:
snFreq = QD X 60 DAYS, snFreq2 = 1, snFreqIV = 1, and snFreqTube = 1, the rest are Null.
if(isset($rowPlanOfCare['snFreq'])){$snFreq = "SN FREQUENCY/DURATION: " . $rowPlanOfCare['snFreq'];}
if(isset($rowPlanOfCare['snFreq2'])){$snFreq2 = " + 2 PRN VISITS FOR ";}
if(isset($rowPlanOfCare['snFreqIV'])){$snFreqIV = "IV COMPLICATIONS";}
if(isset($rowPlanOfCare['snFreqTube'])){$snFreqTube = "TUBE FEEDING COMPLICATIONS";}
if(isset($rowPlanOfCare['snFreqTrach'])){$snFreqTrach = "TRACHEOSTOMY CARE COMPLICATIONS";}
if(isset($rowPlanOfCare['snFreqCath'])){$snFreqCath = "CATHETER CARE COMPLICATIONS";}
if(isset($rowPlanOfCare['snFreqWound'])){$snFreqWound = "WOUND CARE COMPLICATIONS";}
if(isset($rowPlanOfCare['snFreqOstomy'])){$snFreqOstomy = "OSTOMY CARE COMPLICATIONS";}
$snFrequency = $snFreq . $snFreq2 . implode(", ",array($snFreqIV, $snFreqTube, $snFreqTrach, $snFreqCath, $snFreqWound, $snFreqOstomy)) . "\n \n ";
Final Output should only show: SN FREQUENCY/DURATION: QD X 60 DAYS + 2 PRN VISITS FOR IV COMPLICATIONS, TUBE FEEDING COMPLICATIONS < == without the other commas.
I hope you can help me out with this issues. Thanks in advance! :)

Try this:
$snFrequency = $snFreq . $snFreq2 . implode(", ",array_filter(array($snFreqIV, $snFreqTube, $snFreqTrach, $snFreqCath, $snFreqWound, $snFreqOstomy))) . "\n \n ";

Related

Round only int of variable

I am busy with a webshop, and I have an issue:
I display the quantity of the products, and the attribute of the product.
Like:
2,4 cm
2,56 dm
Now I want to Round the numbers of the variable, but if I use:
$qty = round($_product->getBundle_qty());
But if I use: $aantal = $_product->getBundle_qty(); everything works okay, but he doesn't round.
Then the attribute doesn't show anymore ( dm, cm, m ETC)
Can somebody help me?
Is the $_product->getBunlde_qty() actually a double or a float? Or is it just a string? In that case, you can use:
$qty = round(floatval($_product->getBundle_qty()));
Edit:
Now if you want to keep the cm / dm in behind the string we can take the string and explode it at all the spaces, thus creating an array from the string. The last value in this array will be the 'cm' or 'dm' text.
$qtyPieces = explode(" ", $_product->getBundle_qty()); //explode at the space
$qtyPiecesCount = count($qtyPieces); //count all pieces
$qty = round(floatval($_product->getBundle_qty())) . " " . $qtyPieces[$qtyPiecesCount - 1]; //get the last value in the array, count minus 1 because we start counting from 0
Fixed it using explode:
$arr = explode(' ',trim($_product->getBundle_qty()));
echo $arr[1];
It is because the attribute portion is a string.
Round your integers first.
//example:
$qty = round(2.4);
Then add the string attribute back to it.
$value = $qty . "cm";

Tab space issue

I made a simple query system through mySQL which is showing me 100 records and I fetch them into my game but I have probelm with the codes in PHP.
I want to have 5char space between each row So I have to use tab space (\t\t\t\t\t), But I have a problem with this current system (e.g If I have field with two diffrent string value 10char and 2char then use tab space to make space between them I get different results:
2Char string + 5char space = 7Char and 10Char string + 5Char space = 15Char
$query = "SELECT * FROM `scores` ORDER by `score` DESC LIMIT 100";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_results = mysql_num_rows($result);
for($i = 0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
echo $i+1 . "-" . "\t\t Name: " .$row['name'] . "\t\t\t\t Device: " . $row['device'] . "\n \t\t Difficulty: " . $row['level']. "\t\t\t\t Score: " . $row['score'] . "\n\n";
}
Codes Output
1- Name: James Device: HTC OneX
Difficulty: Hard Score: 5760
2- Name: Erika_S Device: PC
Difficulty: Normal Score: 13780
...
My Desired Output
1- Name: James Device: HTC OneX
Difficulty: Hard Score: 5760
2- Name: Erika_S Device: PC
Difficulty: Normal Score: 13780
...
Tab in fact is one char, but displayed in the way that user want. When, for example, in IDE you choose 8 spaces for 1 tab you will get it. There's a fantastic concept called elastic tabstops, but it's only concept - so sad.
Conclusion: you can't do it what you described with tab.
What you can do:
Calculate needed spaces and hardcode with , but it's dirty and you shouldn't do this.
Use html tables
Instead of $row['...'] use sprintf("%-15s", $row['...']), but in each place you'll need to adjust the number (-15) to what's really needed
<?php
$s = 'monkey';
$t = 'many monkeys';
printf("[%s]\n", $s); // standard string output
printf("[%10s]\n", $s); // right-justification with spaces
printf("[%-10s]\n", $s); // left-justification with spaces
printf("[%010s]\n", $s); // zero-padding works on strings too
printf("[%'#10s]\n", $s); // use the custom padding character '#'
printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters
?>
The above example will output:
[monkey]
[ monkey]
[monkey ]
[0000monkey]
[####monkey]
[many monke]
read more at http://www.php.net/manual/en/function.sprintf.php
if you can't use printf, you can easily create your own function that does something similar, and is enough for what you need:
function add_spaces($str, $total_len) {
return $str . substr(" ", 0, $total_len - strlen($str));
}

Phone number format received from twilio

I am storing sms received from twilio in a database so I can use them later. When I did this in the sandbox it worked. However when I upgraded to a regular phone number the number received is the same as was sent to, but +1 (or for xxxxxxxxxx where the x's are the original number, it looks more like 1xxxxxxxxxx+)
I therefore changed the mysql_query to the following: but it is still not working. What can be done to recognize that this is the original phone number?
<?php
$starttime = time();
$number = $_POST['number'];
$number1 = "1" . $number;
$number2 = $number . "1";
$number3 = "+1" . $number;
$number4 = $number . "+1";
$number5 = "+" . $number . "1";
$number6 = "1" . $number . "+";
$number7 = $number."1+";
$received = mysql_query("SELECT * FROM sms_received
WHERE (responder='$number' OR responder='$number1'
OR responder='$number2' OR responder='$number3'
OR responder='$number4' OR responder='$number5'
OR responder='$number6' OR responder='$number6')
AND (body='y' OR body='yes' OR body='Y' OR body='Yes' OR 'yea' OR 'Yea')
AND timestamp BETWEEN ".date('Y-m-d H:i:s', strtotime($starttime))." AND NOW()");
?>
But still, nothing is being received. Any ideas how else I can check whether an sms has been received from the user? I can see in the database that it's there... but the mysql isn't finding it. It worked before, when the number sent was identical to the number received from, but with the added +1 it screws it up. (the code before just had WHERE responder = '$number' and it worked, but the additional variables didn't help it).
Does this code have too many OR's? Is that even a problem?
UPDATE:
Thanks, here is the function I'm using to strip the number down to xxxxxxxxxx format, before saving it to the database:
function checkPhone($responder){
$items = Array('/\ /', '/\+/', '/\-/', '/\./', '/\,/', '/\(/', '/\)/', '/[a-zA-Z]/');
$clean = preg_replace($items, '', $responder);
if (substr($clean, 0, 1) == "1") {
return substr($clean, 1, 10);
}
else {
return substr($clean, 0, 10);
}
}
$number = checkPhone($responder);
Twilio returns numbers in a format called E.164, which is an internationally recognized standard for phone number formatting.
In general, it's best practice to standardize the number to E164 BEFORE you store it in the database. That way you don't have to worry about storing different data with two different copies of the same number - eg 925-555-1234 and (925) 5551234.
Google has a libphonenumber library that will convert numbers for you. It works with Javascript, C++, Java, and Python.
If you are using PHP, and only using US/Canadian numbers, you can write a function to normalize phone numbers, that does something like the following:
- Strip out all non number characters from the phone number
(parentheses, dashes, spaces) - you can use a function like preg_replace
- if the phone number begins with a +1, do nothing
- if the phone number begins with a 1, add a +
- else, add a +1 to the beginning of the number.
- finally, store it in the database.
I hope that helps - please let me know if you have more questions.
Kevin
Your last or is redundantly $number6, it should be $number7.
Aside from that, you can do a few different things, such as in:
responder in ('$number', '$number1', '$number2', '$number3', '$number4', '$number5', '$number6', '$number7')
Or something like this:
responder like '%$number%'
Use a regular expression.
preg_match_all("{[0-9]+}",$number,$m);
$norm_num="+".implode($m[0]);
if(strlen($norm_num)<6)
exit('Too short!');
mysql_query("SELECT * FROM sms_received
WHERE responder='%$norm_num%'
AND body IN ('y','yes','Y','Yes','yea','Yea')
AND timestamp BETWEEN ".date('Y-m-d H:i:s', strtotime($starttime))." AND NOW()");

Obfuscating string values in PHP source code

I want to protect PHP source code at easy way.
here is a example.
$a = "\x46\122" . chr(578813952>>23) . "" . chr(0105) . "\x2d";
$b = "\x73" . chr(847249408>>23) . "" . chr(0162) . "\x69" . chr(0141) . "" . chr(905969664>>23) . "";
$c = "" . chr(0x53) . "" . chr(0105) . "\x52\x56" . chr(0105) . "\x52" . chr(796917760>>23) . "\x4e" . chr(545259520>>23) . "\x4d" . chr(0x45) . "";
it is.
$a="FREE-";
$b="serial";
$c="SERVER_NAME";
Please help me someone to convert this type of string ??
There is 3 type of encryption.
Type[1] : "\x46\122"
Type[2] : chr(0105)
Type[3] : chr(578813952>>23)
Please help me to create a convert function...from PHP string.
thank you !
------------------------ I update question-------------------
OK... I should change question..
I want to create a function.
function1.
$a = "FREE-";
echo function1($a);
---> output
"\x46\122" . chr(578813952>>23) . "" . chr(0105) . "\x2d";
in this function, Function use 3 type of logic at random.
here is 3 type.
Type[1] : "\x46\122"
Type[2] : chr(0105)
Type[3] : chr(578813952>>23)
Could you help me ....
This is a, frankly, stupid way of "protecting" your code. Hopefully you realize that once the code is delivered to the clients, they can simply undo all of this and extract the values themselves?
Use legal means to protect the code. "here's my code, you are not allowed to share it. If you do, I get $50 kazillion dollars and the Droit de Seigneur with your most beautiful daughter, or an extra 200 kazillion in lieue if they're all ugly".
An iron-clad licensing agreement will be far better protection than any cereal-box decoder-ring wet kleenex method you care to apply ever will be.
For further suggestions why this is a waste of your time:
Asked at 8:36, decoded at 8:44. Eight minutes of protection: https://stackoverflow.com/questions/5456462/what-does-this-php-code-do
Asked at 11:01, decoded at 11:17, and very-well analyzed at 11:47. Hacked, what does this piece of code do?
In the first case, I'm willing to bet the majority of the fastest poster's time was spent writing. So feel confident that however you try to obfuscate your code, it'll take only three or four minutes to undo whatever it is you've done.
How much time are you willing to put into obfuscating your code when it'll take someone only a few minutes to undo what you've done? Could that time have been better spent writing awesome features that your customers would love?
ord will get you a character's ASCII value, using that we can generate the 3 things you want.
Type 1: Use dechex to convert int to hex.
$chr = 's';
$hex = dechex(ord($chr)); // 73
Type 2: Use decoct to convert into to octal.
$chr = 'E';
$oct = decoct(ord($chr)); // 105
Type 3: Use the << (shift left) operator.
$chr = 'e';
$bin = ord($chr)<<23; // 847249408

Just a question about str_replace

I have a question about str_replace in PHP. When I do:
$latdir = $latrichting.$Lat;
If (preg_match("/N /", $latdir)) {
$Latcoorl = str_replace(" N ", "+",$latdir);
}
else {
$Latcoorl = str_replace ("S ", "-",$latdir);
}
print_r($latdir);
print_r($Latcoorl);
print_r($latdir); gives :N52.2702777778
but print_r ($Latcoorl); gives :N52.270277777800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Yes, it adds a lot of zeros. Can someone explane this behavior just for the fun of it?
print_r ($latrichting);
give's: N
print_r ($Lat);
This give's the weird long number.
So its probably not the str_replace command, you think ?
$latmin2 = bcdiv($latsec, 60, 20);
$latmin_total = $latmin + $latmin2;
$lat = bcdiv($latmin_total, 60, 20);
$latdir = array("N" => 1, "S" => -1);
$latcoorl = $latdir * $latdir[$latrichting];
Happy New Year.
Your string replace search string has a space before the 'N' while the dumped value looks like it's N:
Not sure what it has to do with all the zeros though.
On my system this code fragment:
<?php
$latdir = ':N52.2702777778';
If (preg_match("/N /", $latdir)) {
$Latcoorl = str_replace(" N ", "+",$latdir);
}
else {
$Latcoorl = str_replace ("S ", "-",$latdir);
}
print_r($latdir);
print_r($Latcoorl);
?>
gives the following result:
:N52.2702777778:N52.2702777778
My best guess is you have something after this code that prints out a serie of 0.
How I would do it; just a variation of Anthony's original answer that keeps everything as numeric and doesn't lapse into string mode.
$Latcoorl = ($latrichting == "N") ? ($Lat) : (-1 * $Lat);
The string operations you did won't generate any 0s.
The 0s have to come from $lat. What did you do with $lat? any division by pi? PHP will try to store the most accurate possible float number in $lat. That's not really a problem, its a correct behavior. Just truncate the number when displayed, or round it up.

Categories