I try to make a virutal money system and dont get the "number_format"
I want it like the number "7951753" and want that output:
795 17 53
Other example:
$money = 10001;
1 0 1
How I can make that?
You may use substr
$mynumber=7951753;
echo substr($mynumber, 0, 3)." Gold ". substr($mynumber, 3, 2)." Silver " . substr($mynumber, 5, 2)." Bronze ";
above code produces output:-
795 Gold 17 Silver 53 Bronze
You may do same with 10001 to get 1 Gold 0 Silver 1 Bronze :)
Have a try!
Sorry, display fail on posting that here:
Number: 7951753
Output: 795 Gold 17 Silver 53 Bronze
Other example:
Number: 10001
Output: 1 Gold 0 Silver 1 Bronze
Related
I'm trying to get the last 9 characters of $span.
$span = "";
foreach($html->find('span') as $element1){
if (strpos($element1->outertext, 'kcal') !== false){
$span .= $element1->outertext.'<br>';
}
}
echo substr($span,-9);
It just show me white page, any suggestions?
Edit:
When i debug with var_dump($span) it shows exactly the following:
string(761) " 1 Porsiyon (Orta) AnçuezSardalya Salatası 319 kcal 1 Su Bardağı Ayran (Yağsız) 41 kcal 1 Su Bardağı Anne Sütü 138 kcal 1 Porsiyon (Orta) Amasya Yöresine Özgü Keşkek 728 kcal 1 Porsiyon (Orta) Anne Kurabiyesi 504 kcal "
use trim() to remove white spaces
so you can write
echo substr(trim($span),-9);
I'm making a form text input box for an inventory list. I type everything into the form and it breaks everything down to be put into SQL(not at the SQL part yet).
Input would look something like this
5-1 1/2 black 90° sch 40 (have 10 of the sch 80 ones)
amount - size - fitting name ( comments)
I got everything in their own variable now I just need to remove the "(comments)" part from the fitting name. str_replace should do the trick but it seems to only work "some times" ? I'm not sure why it doesn't always work. The picture below show that it only worked 2 times. As for my regex, I'm really bad at them. lol Thanks for any help you can give with my little problem.
foreach ($components as $value) {
$value=stripslashes($value);
// get rid of empty lines
if (empty($value)) { continue; }
// will Split the amount of fittings from the value.
$quantity = explode("-", $value);
//Will split the name of the fitting and also give the size of the fittings.
$test=preg_split('/([a-w]|[A-W])/', $quantity[1],2, PREG_SPLIT_DELIM_CAPTURE);
//split the comments from the fitting name.
$comments = explode("(", $test[2]);
//removes the remaining ) from the left side
$comments =preg_replace("/\)/", "" , $comments[1]);
if(!empty($comments)) {
$fitting_name = str_replace("($comments)","", $test[1].$test[2]);
}else{
$fitting_name = $test[1].$test[2];
}
// The table format is just to make sure everything is working right before inputting into SQL
echo "
<tr>
<td>$value</td>
<td>". $quantity[0] ."</td>
<td>$test[0]</td>
<td>$fitting_name</td>
<td>$comments </td>
</tr>
<tr>
";
}
echo "</tr>
</table>";
}
Input
0-1/2 PP union
1- 1 1/2x1 1/4 copper PP fitting red
9- 1 1/2" copper PP 90°
5-2" copper PP 90° (have 10 of the sch 80 ones)
13-2" copper PP Tee (have 10 of the sch 80 ones)
10-1*1*3/4 copper PP tee
60- 3/4" PP cap and chain value
50 - 3/4" PP value (we only have 4 more)
19- 3/4" threaded cap and chain value
0-2" threaded value
0- 2 1/2" threaded value (have 10 of the sch 80 ones)
5- 3/4" black Street 90°
0 - 3/4 black union
0- 1" black union
0-1" black tee
1 - 1 1/2 black union
6-1 1/4 black cap
7-1 1/2" * 1" black bushing
3 - 1 1/2 black coupling
5-1 1/2 black 90° sch 40? (have 10 of the sch 80 ones)
4 - 3/8" rod 6'
4-3/8" rod 10'
6 - 1/2" rod 6'
0-5/8" rod 6"
0-5/8" rod 10'
0-3/4 rod 6'
2 - 1" rod 6'(have 10 of the sch 80 ones)
I would first remove the comment before doing any parsing, to cleanse the input.
You can extract it with a RegEx :
\s*\(.*?\)
\s* matches 0 or more white spaces
\( matches a parenthesis
.*? matches any characters (lazy match)
\) matches a parenthesis
Now, you can replace this by an empty string :
<?php
$input = [
"0-1/2 PP union ",
"1- 1 1/2x1 1/4 copper PP fitting red",
"9- 1 1/2\" copper PP 90°",
"5-2\" copper PP 90° (have 10 of the sch 80 ones) ",
"13-2\" copper PP Tee (have 10 of the sch 80 ones) ",
"10-1*1*3/4 copper PP tee",
"",
"",
"60- 3/4\" PP cap and chain value ",
"50 - 3/4\" PP value (we only have 4 more)",
"19- 3/4\" threaded cap and chain value ",
"0-2\" threaded value ",
"0- 2 1/2\" threaded value (have 10 of the sch 80 ones) ",
"",
"",
"5- 3/4\" black Street 90°",
"0 - 3/4 black union ",
"0- 1\" black union ",
"0-1\" black tee ",
"1 - 1 1/2 black union ",
"6-1 1/4 black cap",
"7-1 1/2\" * 1\" black bushing",
"3 - 1 1/2 black coupling ",
"5-1 1/2 black 90° sch 40? (have 10 of the sch 80 ones) ",
"",
"",
"",
"4 - 3/8\" rod 6'",
"4-3/8\" rod 10'",
"6 - 1/2\" rod 6'",
"0-5/8\" rod 6\"",
"0-5/8\" rod 10'",
"0-3/4 rod 6'",
"2 - 1\" rod 6'(have 10 of the sch 80 ones) "
];
foreach ($input as $value)
{
$newValue = preg_replace("#\s*\(.*?\)#", "", $value);
echo $newValue . PHP_EOL;
}
Output :
0-1/2 PP union
1- 1 1/2x1 1/4 copper PP fitting red
9- 1 1/2" copper PP 90°
5-2" copper PP 90°
13-2" copper PP Tee
10-1*1*3/4 copper PP tee
60- 3/4" PP cap and chain value
50 - 3/4" PP value
19- 3/4" threaded cap and chain value
0-2" threaded value
0- 2 1/2" threaded value
5- 3/4" black Street 90°
0 - 3/4 black union
0- 1" black union
0-1" black tee
1 - 1 1/2 black union
6-1 1/4 black cap
7-1 1/2" * 1" black bushing
3 - 1 1/2 black coupling
5-1 1/2 black 90° sch 40?
4 - 3/8" rod 6'
4-3/8" rod 10'
6 - 1/2" rod 6'
0-5/8" rod 6"
0-5/8" rod 10'
0-3/4 rod 6'
2 - 1" rod 6'
Try it yourself
Plese try changing the following line
$fitting_name = str_replace("($comments)","", $test[1].$test[2]);
to
$fitting_name = str_replace("(" . trim($comments).")","", $test[1].$test[2]);
and see the effect. Please let us know the result.
My raw output of socket_recvfrom is:
ID IP PING IDENTIFIERNUMBER USERNAME
0 127.0.0.1:1234 0 ID123456789 Moritz
1 127.0.0.1:1234 46 ID123456789 August Jones
2 127.0.0.1:1234 46 ID123456789 Miller
It is a single string that contains all of this informations in once and just contains whitespaces between the informations. All keys can be longer or shorter.
My problem:
When I preg_split("/\s+/") it, then I get a good array with useable data, but when the username contains spaces it creates a second index for this. Not good, all data that comes after this just get destroyed.
I sort the array like this: ID, USERNAME, PING, IDENTIFIERNUMBER, IP
Example by the sorting output with username with one space in it:
ID: 0, USERNAME: Moritz, PING: 0, IDENTIFIERNUMBER: ID123456789, IP: 127.0.0.1:1234
ID: 1, USERNAME: August, PING: Jones, IDENTIFIERNUMBER: 46, IP: ID123456789
ID: 127.0.0.1:1234, USERNAME: 2, PING: Miller, IDENTIFIERNUMBER: 46, IP: ID123456789
How do I get the information correctly out of the string?
Just forgot to say:
The string begins with: --------------------------------- in a not countable order. So it can be like 10 characters or 12.
The string ends with:
(8 users in total)
The regex methode looks good. I only need to filter out the other characters.
--------------------------------- 0 127.0.0.1:1234 0 ID123456789(OK) Moritz 1 127.0.0.1:1234 46 ID123456789(OK) August Jones 2 127.0.0.1:1234 46 ID123456789(OK) Miller (7 users in total)
Last problem:
https://www.regex101.com/r/wP8cW1/1
You may use regex
(?P<ID>\d+)\s+(?P<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)\s(?P<PINGR>\d+)\s(?P<IDENTIFIERNUMBER>ID\d+)(\(OK\))?(?P<USERNAME>(\s[A-z]\w+)+)
MATCH 1
ID [0-1] `0`
IP [2-16] `127.0.0.1:1234`
PINGR [17-18] `0`
IDENTIFIERNUMBER [19-30] `ID123456789`
USERNAME [31-37] `Moritz`
MATCH 2
ID [39-40] `1`
IP [41-55] `127.0.0.1:1234`
PINGR [56-58] `46`
IDENTIFIERNUMBER [59-70] `ID123456789`
USERNAME [71-83] `August Jones`
MATCH 3
ID [85-86] `2`
IP [87-101] `127.0.0.1:1234`
PINGR [102-104] `46`
IDENTIFIERNUMBER [105-116] `ID123456789`
USERNAME [117-123] `Miller`
Demo and explanation
Do you alredy try explode the string by new lines \n ??
test this code.
$str = '0 127.0.0.1:1234 0 ID123456789 Moritz
1 127.0.0.1:1234 46 ID123456789 August Jones
2 127.0.0.1:1234 46 ID123456789 Miller';
$lines = array_filter(explode("\n", $str));
foreach ($lines as $value) {
$t[] = preg_split("/\s+/", trim($value));
}
Now in the var $t you have a usefull data.
I have the below text, and I want a regular expression pattern for it:
qos policy 1024SharedUnlimitedUserRX pwfq
rate maximum 1792
weight 16
num-queues 4
queue-map Fastweb
congestion-map Fastweb
queue 0 priority 0 weight 100
queue 1 priority 1 weight 90
queue 2 priority 2 weight 70
queue 3 priority 3 weight 85
The pattern should fetch the integer value after the first weight phrase. In other words I want the 16 value, but not value of the next 4 lines(100, 90, 70, 85).
I wrote this pattern:
/weight (\d*)/
but this pattern found value of the other lines too, what should I do?
Note: I used preg_math() function, not preg_match_all()
This regex would perhaps suit you:
preg_match('/^weight (\d+)/m', $input, $match);
regex101 demo
^ makes sure that the weight is at the beginning of the line.
The m flag makes ^ match at the beginning of each line instead of only at the beginning of the string.
If there can be other weight you don't want which can be at the beginning of the line, you can try this instead, which makes sure that the weight is just after maximum:
preg_match('/maximum \S*\s*weight (\d+)/m', $input, $match);
regex101 demo
Do like this
<?php
$input_line="qos policy 1024SharedUnlimitedUserRX pwfq
rate maximum 1792
weight 16
num-queues 4
queue-map Fastweb
congestion-map Fastweb
queue 0 priority 0 weight 100
queue 1 priority 1 weight 90
queue 2 priority 2 weight 70
queue 3 priority 3 weight 85";
preg_match("/(?:weight\s(\d+)(?!\S+))/", $input_line, $output_array);
print_r($output_array);
DEMO
OUTPUT
Array
(
[0] => weight 16
[1] => 16
)
You can use this regex
(?<=weight )\d+
Check the regex at http://regex101.com/r/eP2yS2
DEMO
<?php
$input_line="qos policy 1024SharedUnlimitedUserRX pwfq
rate maximum 1792
weight 16
num-queues 4
queue-map Fastweb
congestion-map Fastweb
queue 0 priority 0 weight 100
queue 1 priority 1 weight 90
queue 2 priority 2 weight 70
queue 3 priority 3 weight 85";
preg_match("/(?<=weight )\d+/", $input_line, $output_array);
print_r($output_array);
?>
OUTPUT
Array
(
[0] => 16
)
Link : https://eval.in/109758
rate maximum \d+[\s\S]+?weight \d+
I know question title seems quite 'un-understandable', but I don't know how to write question title for this particular question.
Question:
I want to find factor for position.
Let me clear you with an example.
Value Factor
[Available] [Have to find out]
----------------------------------
1 10
3 10
9 10
10 10
11 10
25 10
50 10
75 10
99 10
100 100
101 100
105 100
111 100
127 100
389 100
692 100
905 100
999 100
1000 1000
1099 1000
1111 1000
4500 1000
6825 1000
7789 1000
9999 1000
10000 10000
10099 10000
51234 10000
98524 10000
99999 10000
100000 100000
and so on.
I hope you understand what I mean to get.
Assuming that the first three values should be 1 (as noted by Asaph), then you just need to use all that logarithm stuff you learned in school:
pow(10, floor(log10($n)))
So, how does this work? The base-10 logarithm of a number x is the y such that 10^y = x (where ^ stands for exponentiation). This gives us the following:
log( 1) 0
log( 10) 1
log(100) 2
...
So the log10 of a number between 1 and 10 will be between 0 and 1, the log10 of a number between 10 and 100 will be between 1 and 2, etc. The floor function will give you the integer part of the logarithm (we're only dealing with non-negative values here so there's no need to worry about which direction floor goes with negative values) so floor(log10()) will be 0 for for anything between 1 and 10, 1 for anything between 10 and 100, etc. Now we have how many factors of ten we need so a simple pow(10, ...) gives us the final result.
References:
log10
floor
pow
I'm still a little unsure of what you're asking, but it seems like you want to map values to other values... In php arrays can be indexed with anything (making them a map). If 999 always means a factor of 100 and 1099 always means a factor of 1000, you can set the value of array[999] to 100 and the value of array[1099] to 1000, etc.
Basically Factor is 10 to the power of number of digits in $value minus 1 (except for the single digit numbers):
if($value < 10) {
$value += 10;
}
$numOfDigits = count(str_split($value,1));
$factor = pow(10,$numDigits-1);
This function should work for you. It seems like the first 3 "factors" in your list don't fit the pattern. If, in your sample data set, those first 3 "factors" should really be 1 instead of 10, then you can safely remove the first 3 lines of the body of the function below.
function getFactor($num) {
if ($num < 10) { // If the first 3 "factors" listed
return 10; // in the question should be 1 instead of 10
} // then remove these 3 lines.
$factor = 1;
while($factor <= $num) {
$factor *= 10;
}
return $factor / 10;
}