Extract number and periods from string in php
Discuss coding issues, and scripts related to PHP and MySQL.
-
Marius
- Posts: 107
Extract number and periods from string in php
I have a string with numbers and periods. For example '123456...'
I want to separate 123456 and ... and still get the number and the period in php.
I tried using preg_replace():
Code: Select all
$numbers = preg_replace('/[^0-9]/', '', '123456...');
$period = preg_replace('/./', '', '123456...);
With the code above I can extract the numbers but I can't extract the periods. Is there any other way to extract the periods?
Admin
It's because the dot (.) in regex is any character, while \. is literal dot.
Try the following code:
Code: Select all
$matches = null;
$input = '123456...';
preg_match('/(?<nums>\d+)(?<periods>\.+)/', $input, $matches);
$numbers = $matches['nums'];
$periods = $matches['periods'];
$nums_only = preg_replace('/[^\d]/', '', $input);
Similar Topics
-
Converting a salary input string to number
JavaScript - jQuery - Ajax
First post
I have the following problem:
1. eliminate thousand separators(,) 5,555 to 5555.
2. if the user inputs 55,66 replace , with . and get the value...
Last post
Try the function from the following code:
function salNr(s){
//If there is comma before the last two digit, replace it with dot, else remove...
-
Adding string from database into PDF
PHP - MySQL
First post
Hello Coursesweb I have a problem with my php document to convert it to PFD
I cant find out how to get results from $users into the HTML to the PDF...
Last post
Thanks MarPlo I prefer not to ask anything on stackoverflow.com
because I once typed a small i instead of I.
and received many comments about it....
-
Remove backslash from string
PHP - MySQL
First post
How can I remove backslashes from string in php?
I tried the following code.
$str ='abc-\123';
$str = stripcslashes($str);
echo $str; //...
Last post
You can use str_ireplace() to remove backslash from string in php, but like in the following code (add two backslashes into the removing argument):...
-
Move the string that matches the regex to the end
JavaScript - jQuery - Ajax
First post
I have the following problem to solve it in JavaScript:
- Find the text which is between small brackets and shift that text with the brackets to...
Last post
Try the following code:
function testToEnd(str){
//get matched string
let st = str.match(/ *\( +\) */g)
if(st){
st = st ;
// replace the...
-
Check for an array of words in a string in php
PHP - MySQL
First post
I have a list of spam words that are into an array. When a user submits a string text, I want to know if it contains these words. How can I do this...
Last post
You could add the spam words into a string, with | as word separator and then use regular expression to check.
$my_words =...