How to extract integer and decimal numbers in PHP string correctly?

How to extract integer and decimal numbers in PHP string correctly?

By : user3099545
Date : January 11 2021, 03:32 PM
it helps some times You're using a . for thousands and a , for decimals, and it looks like your text is intended for humans so there will always be a number immediately before a . and ,.
So what you want is 1 or more numbers, followed by 0 or more thousand-groups (a . followed by 3 more numbers), optionally followed by exactly one , and one or more numbers.
code :
One or more numbers: [0-9]+
0 or more thousand-groups: (\.[0-9]{3})*
Optionally a `,` and one or more numbers: (,[0-9]+)?
$string = 'María vive en un pueblo de 25 957 habitantes y cobra 1859 euros al mes. OJO: no sé si os habéis fijado, pero los números del último ejemplo 
no llevan un punto o una coma separando los millares (25.957 o 1.859). Sé que resulta extraño, pero la nueva normativa de la R.A.E. 
dice que los números de cuatro cifras NO llevarán separación (1859) y los números de cinco cifras o más NO llevarán ni puntos ni comas, 
sino una separación (25 957 o 1 343 392). El 94% de los niños ha traído los deberes hechos. He pagado $50,95 dólares.';

$matches = [];
preg_match_all('/[0-9]+(\.[0-9]{3})*(,[0-9]+)?/', $string, $matches);


    [0] => 25
    [1] => 957
    [2] => 1859
    [3] => 25.957
    [4] => 1.859
    [5] => 1859
    [6] => 25
    [7] => 957
    [8] => 1
    [9] => 343
    [10] => 392
    [11] => 94
    [12] => 50,95

Share : facebook icon twitter icon
Extract decimal or Integer from a string in php

Extract decimal or Integer from a string in php

By : Avinash Prakash
Date : March 29 2020, 07:55 AM
this one helps. Eg: , This should do it:
code :
    $str = '';
    $str .= "Additional tax(2.34)";
    $str .= "Additional tax(3)";

    if (preg_match_all('/\((\d+(?:\.\d+)?)\)/', $str, $matches) > 0) {
Extract multiple decimal numbers from string in T-SQL

Extract multiple decimal numbers from string in T-SQL

By : Bryan
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Assuming your data is consistent, this should work fine, and has the added advantage of being easier on the eyes. Also consider decimal if you're going for precision.
code :
  cast(left(r, charindex('%', r) - 1) AS float) as minVal,
  cast(replace(right(r, charindex('-', r) - 1), '%', '') as float) AS maxVal
from ( select '22.45% - 42.32%' as r ) as tableStub
Extract numbers and decimal from string in EXCEL

Extract numbers and decimal from string in EXCEL

By : Er Dhiraj Sharma
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have an Excel file that contains roughly 40,000 rows of data and i am trying to create a new columnn of only the number and decimal portion of a strinng in another column. I tried to TRIM() and LEFT() but the number value size effects that extraction. I also saw another post in StOv that extracts numbers only but my goal is to grab the decimals so I can see the exact value. Here is an example of the content of one cell. for any help!!! , With data in A1, use:
code :
How to correctly extract string in decimal

How to correctly extract string in decimal

By : Naifous
Date : March 29 2020, 07:55 AM
Hope that helps I can have different element like this inside a string , Tk Finish to find solution Work fine with these elements
code :
  preg_match_all('!\d+!', $price, $matches);
  $price_extracted =  (float)implode('.', $matches[0]);
Extract decimal numbers from string in Sparklyr

Extract decimal numbers from string in Sparklyr

By : user2892441
Date : March 29 2020, 07:55 AM
this will help I've been trying to extract decimal numbers from strings in sparklyr, but it does not work with the regular syntax you would normally use outside of Spark. , You could use regexpr from base R
code :
v <- "$170.5M"
regmatches(v, regexpr("\\d*\\.\\d", v))
# [1] "170.5"
Related Posts Related Posts :
  • How to search a row that has been joined with another table
  • How to do mysql LEFT JOIN for Google multiple line chart (php, mysql)?
  • Find the two longest strings separated by dash in PHP
  • Cannot access private property with Set method in Symfony
  • How to compare 2 array of PHP objects then fill in empty string if data not match?
  • strtotime gives back false result to strings in an array
  • How to create Url that contain name and id laravel
  • Codeigniter: Models not working with an error: Undefined property: CI_Loader
  • How to use + or - operators as parameters in Doctrine prepared SQL statement?
  • PHP unpack overlfow variable memory limit
  • How to check the URL's structure using PHP preg_match?
  • PHP preg_replace - text will not be recognized
  • Show image from other domain without showing the domain name - PHP
  • How to run and watch local server at the same time?
  • Access form request from Observer laravel
  • single quote problem with preg_replace_callback
  • How to Create A Unique URL for each product enquiry form for Google Analytics Tracking
  • Laravel email configuration: what am I missing?
  • Assign indexed array values to multidimensional array?
  • Custom WordPress stylesheet not loading
  • Data attribute only returning first value
  • How to list all roles with Permissions,in spatie permission Package?
  • How to achieve this number pattern with n input
  • How to iterate array inside array data in laravel ,output are given below
  • To get output of movie with its genre
  • Extracting some data from a JS object literal string in PHP using Regex
  • axios and vuex are deleting wrong index in array
  • Setting empty CURLOPT_POSTFIELDS
  • Laravel 5.8 use subdomain as API endpoint beside domain.com/api
  • Error while add data in database SQL Server 2016
  • Does PhpStorm allow to skip PHPDoc tags when type hints is declared?
  • html classes are changed to Attributes
  • Using json to import multidimensional data
  • passing data from the view to the controller, where the data comes from another controller
  • Parsing Sub-Object from an XML Feed with objects
  • Getting error "Unknown column 'text' in 'field list'"
  • How to properly get onedrive access token via php and curl
  • How to add spell check to a php function
  • Is it posible to create css rules with php?
  • Pretty URL with $_GET method
  • Wordpress always load same page - index.php
  • Find value in array and return with new from different array
  • PHP multiple file upload with different keys
  • Can't modify times one a time without weird errors
  • Get size of scraped image with domcrawler (Goutte)
  • Laravel - Ordering a collection of models
  • PHP Fatal error: Uncaught Error: Call to a member function setFetchMode() on boolean
  • Strict Standards: Only variables should be passed by reference in /main_dir/sub_dir/backup.php
  • Uncaught Google_Exception: (update) unknown parameter: 'copyRequiresWriterPermission'
  • Laravel 5 form does not redirect with old input
  • How to get values greater than or equal to in Mysql database sql when its a value like 1:00 PM?
  • PHP datediff overday issue
  • Masking an auto incrementing primary key
  • How to grab a String BEFORE a certain word
  • ZF2 Routing Multiple Controllers based on route constraints
  • How to deregister/dequeue jquery.sticky.js in Elementor (Pro)?
  • How to fix errors counting words in text plain with PHP?
  • How to get View data in Laravel in custom helper or directive
  • Creating a new data entry into a database with eloquent and trying to access the variable in the url
  • How to test authentication using Laravel Dusk?
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org