i've a string like
$input="16°28'60,00''"
thats is on my db and stored as TEXT utf8_general_ci
im trying to convert it to decimal/lat-long system. So I write a function that splice the input and convert it.
Im using $input as an array, and when is on position 2, I have a strange result thats broke my function:
$input[2]---> 'b"Â"'
in position 2 there is the "°"
the next row check if esist "°" but due this error can works
if($tempD == iconv("UTF-8", "ISO-8859-1//TRANSLIT", '°')
how can i fix that?
If the format of the DB string is always the same, just grab the digits out and you don't need to bother with the degrees, minutes, seconds.
$input = "16°28'60,00''";
preg_match_all("/(\d+)/", $input, $match);
print_r($match);
Output:
Array
(
[0] => Array
(
[0] => 16
[1] => 28
[2] => 60
[3] => 00
)
[1] => Array
(
[0] => 16
[1] => 28
[2] => 60
[3] => 00
)
)
Now you have each digit and you can convert it easily.
Related
I need to figure out a method using PHP to chunk the 1's and 0's into sections.
1001 would look like: array(100,1)
1001110110010011 would look like: array(100,1,1,10,1,100,100,1,1)
It gets different when the sequence starts with 0's... I would like it to segment the first 0's into their own blocks until the first 1 is reached)
00110110 would look like (0,0,1,10,1,10)
How would this be done with PHP?
You can use preg_match_all to split your string, using the following regex:
10*|0
This matches either a 1 followed by some number of 0s, or a 0. Since a regex always tries to match the parts of an alternation in the order they occur, the second part will only match 0s that are not preceded by a 1, that is those at the start of the string. PHP usage:
$beatstr = '1001110110010011';
preg_match_all('/10*|0/', $beatstr, $m);
print_r($m);
$beatstr = '00110110';
preg_match_all('/10*|0/', $beatstr, $m);
print_r($m);
Output:
Array
(
[0] => Array
(
[0] => 100
[1] => 1
[2] => 1
[3] => 10
[4] => 1
[5] => 100
[6] => 100
[7] => 1
[8] => 1
)
)
Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 1
[3] => 10
[4] => 1
[5] => 10
)
)
Demo on 3v4l.org
I have a string which returns data usage in B, KB, MB and so on. I want to separate the number and string part of it.
Ex : 14.5 MB - I want 14.5 and MB separately.
I tried using regex
'/(\d+)(\w)/'
but does not give the desired result.
Expected result
Array( [0] => 14.5, [1] => 'MB' )
You can make use of explode function if you are sure that your string will always contains space in between
$consumedData = '14.5 MB';
$withSeperate = explode(' ',$consumedData);
var_dump($withSeperate);
Output
Array([0] => 14.5, [1] => 'MB');
You may try using preg_match_all here with the following regex pattern:
\b(\d+(?:\.\d+)?)\s*([KMGT]?B)\b
This matches a (possibly) decimal size, followed by a unit based in bytes.
$input = "Here are three sizes 14.5 MB, 10B, and 30.8 GB";
preg_match_all("/\b(\d+(?:\.\d+)?)\s*([KMGT]?B)\b/", $input, $matches);
print_r($matches[1]);
print_r($matches[2]);
This prints:
Array
(
[0] => 14.5
[1] => 10
[2] => 30.8
)
Array
(
[0] => MB
[1] => B
[2] => GB
)
I have a column pack_size in a table called product_master_test. The problem that I am facing is that the pack_size is in mixed formats, there is no uniformity to it.
For example:
4 x 2kg (pack size should be 4)
48-43GM (pack size should be 48)
12 x 1BTL (pack size should be 12)
1 x 24EA (pack size should be 24)
I've been thinking about different approaches, but I can't think of anything that would work without having a lot of IF statements in the query/PHP code. Is there a solution that I am missing?
I do have the file in Excel, if there is an easier way to process it using PHP.
I am not including any code, as I'm not entirely sure where to start with this problem.
Using a regex to split the pack size could at least give you the various components which you can then (possibly) infer more from...
$packs = ["4 x 2kg","48-43GM","12 x 1BTL","1 x 24EA", "12 X 1 EA"];
foreach ( $packs as $size ) {
if ( preg_match("/(\d*)(?:\s+)?[xX-](?:\s+)?(\d+)(?:\s+)?(\w*)/", $size, $match) == 1 ) {
print_r($match);
}
else {
echo "cannot determine - ".$size.PHP_EOL;
}
}
(regex can probably be optimised, not my area of expertise). It basically splits it to be a number, some space with either a x or a - and then another number followed by the units (some text). The above with the test cases gives...
Array
(
[0] => 4 x 2kg
[1] => 4
[2] => 2
[3] => kg
)
Array
(
[0] => 48-43GM
[1] => 48
[2] => 43
[3] => GM
)
Array
(
[0] => 12 x 1BTL
[1] => 12
[2] => 1
[3] => BTL
)
Array
(
[0] => 1 x 24EA
[1] => 1
[2] => 24
[3] => EA
)
Array
(
[0] => 12 X 1 EA
[1] => 12
[2] => 1
[3] => EA
)
With the else part it should also give you the ones it cannot determine and perhaps allow you to change it accordingly.
You could present an associative array of all the strings from the table as keys corresponding with correct pack_size you desire.
$packsize = ["4 x 2kg" => 4, "48-43GM" => 48, "12 x 1BTL" => 12, "1 x 24EA" => 24]; //add all pack_sizes here
echo $packsize["4 x 2kg"]; // Output: 4
Now you could get the acutal pack size via the key of associative array. It could save some time you would spend making if/else conditions or switching the input. I'm not sure if there is something wrong with this approach, so correct me if so.
I am trying to learn how to write a RegEx but it seems all my searches lead to unclear information. So my question is two fold.
Does anyone have a good source on how a newbie could learn to write RegExs?
How could I write a RegEx that breaks the string 1y 311d 16h 42m into variables?
I'm looking to take the above text string and break it into something like:
$duration[years] = 1;
$duration[days] = 311;
$duration[hours] = 16;
$duration[minutes] = 42;
Please note the total digits might may not always be the same for example it could be two digit days. Something like. 25d or some could be omitted. I might just get days and hours. Lastly the order might change. Perhaps it is written days then years etc.
I know I could do this easily with an explode function and strpos, but I really want to learn Regex so I am using this as an example as I understand they can be very powerful for things like this.
1) Some useful pages:
http://www.regular-expressions.info
https://regex101.com/
http://php.net/manual/en/book.pcre.php
2) Specifically, this:
$pattern = '/(?:(?P<years>\d+)y\s*)?(?:(?P<days>\d+)d\s*)?(?:(?P<hours>\d+)h\s*)?(?:(?P<minutes>\d+)m\s*)?/';
preg_match($pattern, '1y 311d 16h 42m', $duration);
print_r($duration);
// Array
// (
// [0] => 1y 311d 16h 42m
// [years] => 1
// [1] => 1
// [days] => 311
// [2] => 311
// [hours] => 16
// [3] => 16
// [minutes] => 42
// [4] => 42
// )
preg_match($pattern, '311d 42m', $duration);
print_r($duration);
// Array
// (
// [0] => 1y 311d 16h 42m
// [years] =>
// [1] =>
// [days] => 311
// [2] => 311
// [hours] =>
// [3] =>
// [minutes] => 42
// [4] => 42
// )
This will give fixed order though. If the order can change, regexp is not a good tool. It's still possible in this case, but rather awkward. Here it is:
$pattern = "/(?=.*?(?:(?P<years>\d+)y|$))(?=.*?(?:(?P<days>\d+)d|$))(?=.*?(?:(?P<hours>\d+)h|$))(?=.*?(?:(?P<minutes>\d+)m|$))/";
preg_match($pattern, '311d 16h 1y', $duration);
print_r($duration);
// Array
// (
// [0] =>
// [years] => 1
// [1] => 1
// [days] => 311
// [2] => 311
// [hours] => 16
// [3] => 16
// )
Entering these patterns (without the leading and trailing slashes) in regex101 will give you the explanation of what exactly it is trying to match. Find other examples from the regex tag questions and enter them as well, and try to see how they work. Experience is the best teacher.
A webservice returns a timestamp field in base64Binary format. It looks like this in SOAP response:
<a:TimeStamp>AAAAAAMpI9Q=</a:TimeStamp>
PHP __soapCall, however, b64_decode()s that and I get a binary string looking like ')#▒'. How do I get actual timestamp out of this? I tried to unpack('L') it but it gives me Array([1] => 0) as a result. Is there really zero i.e. 1970-01-01 or have I missed something?
This test program:
$b = "AAAAAAMpI9Q=";
$ts = base64_decode($b);
print_r(array_map("ord", str_split($ts)));
outputs:
Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 3
[5] => 41
[6] => 35
[7] => 212
)
showing that the base64-encoded string gives you an 8-character string when unpacked. So presumably it represents a 64-bit integer, which might be signed or unsigned, and no, it isn't zero.
Given the values above it looks like the value is 53027796 - is that what you're expecting?