How to format numbers using Regular Expression and PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am practicing for regular expressions.
I was trying to format numbers using PHP and regex. I want to add comma after each 3 digits like this 111222333444 to this format 111,222,333,444 or 11222333444 to 11,222,333,444 by using PHP and Regular expression.
I searched a lot but I could not find exact solution for this.
I know that there is function in php (number_format) to do this but I want to use Regular expression and PHP to do this because I am learning regex and practicing so I want to use regex and php only.

Here is a regex based solution:
$repl = preg_replace('/(?!^)(?=(?:\d{3})+$)/m', ',', $input);
RegEx Demo
Explanation:
(?!^) - Negative lookahead to make sure we are not at start of input
(?=(?:\d{3})+$) - Positive lookahead to make sure there 1 or more of 3 digit sets following current position
Replacement is just a literal comma
More explanation is available at linked demo

Related

Why is the Regex for my product's code not working? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to do Regex code for product's code in VSCode's HTML. My product's code has the following conditions:
Required enter 6 characters
First 2 characters must be letter and uppercase
Next 4 characters must be numbers.
I have tried this regular expression and it doesn't work:
^[A-Z]{2}+\[0-9]{4}$
Your regex should be:
^[A-Z]{2}[0-9]{4}$
This corrects the escaping of your character class; that made it no longer a character class but a series of characters to match in the regex, ending with 4 ]s. The + also is not needed as the {2} is stating only 2 uppercase alpha characters are allowed.
You can also swap the [0-9] with \d which is the metacharacter for an integer. With PHP regexs you also need delimiters so something like:
/^[A-Z]{2}\d{4}$/
could be used in preg_match.

what does || symbol means in Preg_replace() PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I was reading about how to make preg_replace() act like eval() function if we put the modifier /e this is my code:
$fa= '/site'.$_GET['1st'];
$sh= $_GET['replace'];
$ka= 'admin the best over the rest';
echo preg_replace($fa,$sh,$ka);
if the code running on site, it looks like :
www.site.com/a.php?1st=//e&replace=phpinfo();
but there is a problem that the modifier /e mustn't followed by any thing so it will work if we put || like this :
www.site.com/a.php?1st=||//e&replace=phpinfo();
so here is my question what is || here and how it works ??
im using windows 10 and php version 5.2
| separates alternatives in the regexp; e.g. /abc|def|ghi/ matches either abc, def, or ghi.
When you write 1st=||//e the resulting regexp will be /site||//e. Two of the alternatives are empty strings, which will match the empty strings before and after each character. So this will call phpinfo() for each character in $ka.
Actually, you should get an error because you have two / at the end of the regexp. It should be 1st=/e or 1st=||/e.

PHP - preg_match explanation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Im a beginner in PHP I just want to ask can someone explain to me this line of code.
(preg_match('/^\w{5,}$/', $username))
Thankyou in advance. :) Your answer is so much appreciated. :)
Your PHP match string is
/^\w{5,}$/
and a PHP match string is surrounded by / characters which are not part of the RegEx string itself.
According to the comments your problem is about understanding regular expressions, not PHP.
^ is the beginning of the line, correct
$ is the end of the line, correct
\w Any word character (letter, number, underscore)
a{5,} does mean 5 or more characters 'a'
Therefore: If there are 5 or more any word characters in the username the function returns a positive result.
Or even easier: A username needs to contain at least five any word characters.
Learn more about regular expressions and how they work. Some explanation can be found in this comment.

Find Specific IP Address in a String with PHP preg_match() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
My PHP code needs to decide whether or not a specific IP address is in the string $ipaddress:
preg_match("[1][4]\.[1][0][8]\.[2][4][1]\.[6][9]", $ipaddress )
This doesn't work for some reason. Any ideas why it doesn't work?
[1] and 1 is the same thing, there is no point in creating groups of one element. You can look straight for 14\.108\.241\.69.
What is missing for this to work are the delimiters. When using Perl regular expressions the pattern must be enclosed by delimiters, typically /, # or ~.
preg_match('/14\.108\.241\.69/', $ipaddress)
preg_match('~14\.108\.241\.69~', $ipaddress)
preg_match('#14\.108\.241\.69#', $ipaddress)
Are all valid uses of preg_match().
Because you are looking for a simple piece of string, you don't really need preg_match(), a simple strstr($ipaddress, '14.108.241.69') !== FALSE will do the job just as well and faster.

Making all occurrences of a specific word capital? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got a few recurring words on my website that appear quite frequently. Is there any way to make all occurrences of that specific word all capitals?
You can use str_ireplace. It's the case-insensitive version of str_replace. Once you have the content from the database inside of a variable you can do the following before you output it:
$content = str_ireplace('replace', 'REPLACE', $content);
This is searching for the case-insensitive text replace inside of $content and changing all occurrences to REPLACE. This will not only change whole words, it will change partials as well, for example it will change replaced to REPLACEd.
If you would like to only select whole words you will need to use regular expressions with the preg_replace function. Example:
$content = preg_replace('/(\W|^)replace(\W|$)/i', '${1}REPLACE${2}', $content);
This matches all case-insensitive occurrences of the word replace that are either next to a non-word character (/W) or the beginning (^) or end ($) of a line, and replaces it with REPLACE plus whatever was before it (${1}) and after it (${2}).

Categories