I don't know if it's possible but i want to achieve this:
i have lot of product id's, each one being a different variable: $p1, $p2, $p3, etc.
I'm using that variable to call images and links, so that's quite simple (the images names are the same than product id).
The structure of the names are: 1234-123 . The first sequence can be 3 or 4 numbers lenght, and the second sequence can be 2 or 3 numbers lenght.
i.e:
$p1 = '1234-123'
$p2 = '123-12'
I need to display those products ids in a certain format: Product($p1): 1234 (123)
So, my concrete question is:
How can i separate this 2 sequences? i mean, something like:
PRODUCT: <?php echo from first value to "-"?> (<?php echo from "-" to end of variable value ?>)
Use explode():
list($firstPart, $secondPart) = explode('-', $p1);
Related
in my current project I have an array, which is e.g. "118" outputs. after the first number (at 118 the first 1) I would like to insert a comma.
I have already tried to use the "number_format ()" function to put a comma, but it always puts the comma at the end of the array.
My current code:
$api = "https://r6tab.com/api/player.php?p_id=";
$scenus = "3bb8d3bc-ab7a-45fb-8154-ed54897b2c4c";
$api_response_1 = file_get_contents($api.$scenus);
$scenus_kd = $api_response_1_decoded['kd'];
... <h4><?php echo number_format($scenus_kd, 2); ?></h4> ....
My current output:
118.00 but it should be spent like this 1.18
The number format doesn't insert a decimal point or comma. It rounds off the number. You used
number_format($scenus_kd,2)
That rounds off 118 to two decimal places, which is 118.00. If you want to turn 118 into 1.18, you need to divide it by 100:
number_format($scenus_kd/100,2)
I am looking to count the length of strings in multiple variables and add them together to get the total count.
I have tried strlen but have either messed up the syntax or have not used the proper code.
//$_SESSION['var1'] and $_SESSION['var2'] will each be numbers from -30.0 to 1000.0. I need the lengths of the two variables to be added. I need the negative sign(s) and decimal separators, or dots, to be counted.
$_SESSION['var_array'] = $_SESSION['var1'].$_SESSION['var2'];
$_SESSION['var_count'] = strlen($_SESSION['var_array']);
or
$_SESSION['var_count'] = strlen($_SESSION['var1'])+strlen($_SESSION['var2']);
Various results are observed. Sometimes the correct number IS observed but usually not.
The problem is that just storing numeric values doesn't maintain trailing decimals if they are 0. You could alternatively store them as strings, which will maintain the values exactly as you want them or format the numbers to ensure they contain the right format. The following code shows what I mean...
$_SESSION['var1'] = -30.0;
echo $_SESSION['var1'].PHP_EOL; // Gives -30
$_SESSION['var1'] = number_format($_SESSION['var1'], 1);
echo $_SESSION['var1'].PHP_EOL; // gives -30.0
$_SESSION['var2'] = "1000.0";
echo $_SESSION['var1'].PHP_EOL; // gives 1000.0
$_SESSION['var_array'] = $_SESSION['var1'].$_SESSION['var2'];
echo strlen($_SESSION['var_array']); // gives 11
I am getting value from a API to get the amound spend.
Currently I got this:
Total invested: € 17101
Total invested(formatted): € Eu 17101,00
What I want:
I want to make the total invested string (fx 17101) to a currency number (171,01).
I have tried to do it with:
number_format($jsonData->amount_spent, 2, ',')
Sadly nothing was given the result I wanted
If the input is a string '17101' in Euro-Cents devide by 100 and then do the format:
$input = "17101";
echo number_format($input/100, 2, ',', '');
// output: 171,01
Also note that
This function accepts either one, two, or four parameters (not three):
from the Docs
I have a text area in my html form.I am collecting the data from this form using POST method.Here I need to set a blank line as a boundary to repeat a function using this form data.
for example I am calculating the sum of the digits which are collected from this text area using below code
<?php
$devices = $_POST['devs'];
$count = array_sum(explode("\n", $devices));
echo "sum is $count";
?>
If I enter below digits
1
2
3
I will get output like:
sum is 6
and what I need is, if I put digits like
1
2
3
4
5
6
I need output like
sum is 6
sum is 15
how can I do it ?
If there isn't any extra whitespace in your data, this can be accomplished very similarly to your current approach, by adding an extra step to explode on two newlines, and then calling your current code on each part:
$devices = $_POST['devs'];
$repeats = explode(PHP_EOL.PHP_EOL, $devices); // Favor PHP_EOL (end of line) to avoid cross OS issues
foreach($repeats as $repeat)
{
$count = array_sum(explode(PHP_EOL, $repeat));
echo "sum is $count".PHP_EOL;
}
Obviously, if there is extra whitespace, then you'll need to do a cleanup step first.
This is kind of a follow on from this post: Regex for splitting params out using preg_match
I have this string 1 0 61 12345678 sierra007^7 0 0 123.123.123.123:524 26429 25000 and I need to get each element. It was suggested I use explode which was a great simple solution but now I need to allow spaces in one of the fields.
Someone else posted this regex:
/^([-0-9]+)\s+([-0-9]+)\s+([-0-9]+)\s+([-0-9]+)\s+(\S+)\s+([-0-9])\s+([-0-9]+)\s+([-0-9.:]+)\s+([-0-9.]+)\s+([-0-9.]+)/mx
That does everything else and I was wondering if it could be modified to allow spaces in field 5 (sierra007^7). The only advice I can offer is that the rest of the fields are always numeric (or a colon as you can see) before and after field 5. Is this possible with 1 regex statement or do I need to parse it in PHP and fudge it together?
EDIT: For example, field 5 could be sierra007^7 OR si erra007^7 or si er ra007^7. It would know that it came across field 5 as its the only one that contains a-zA-Z characters. It would know where field 5 ends because field 6 only contains 0-9 characters.
Thanks.
Why not use explode, like the other thread. And count the number of items in the array. If more items are in the array, you put item 5 + any number too high together again with implode..
Eg. your normal row has 10 items. If the resulting explode has 15 items, you:
implode(" ",array_slice($array,5,(count($array)-10)));
If the number of fields never changes, and there's always a value for each field, you can do it using code below:
$fields = explode (' ', $str);
$defaultNumFields = 10;
if (count($fields) > $defaultNumFields) {
for ($i = 5; $i < (count($fields) - $defaultNumFields) + 5; $i++) {
$field[4] .= ' '.$field[$i];
unset($field[$i]);
}
}
$fields = array_values($fields);
That should do it. I might have mis-calcuated and you might need to change the +4 to a +5, test it on a few strings and let me know.