Replace repeating characters with only one character in php - php

I want to check if a string contains more than or equals 3 times a letter/number and replace it with only one letter/number. For example:
IIIII havvvvve a bigggg tesssssttttt tomorrow soooo iiii 2222551111 haveeee to do this rightttttt
To became like this
I have a big test tomorrow so i 2551 have to do this right.
How can this be done with preg_replace ?

Regex:
([A-Za-z0-9])\1\1+
This would match more than or equals 3 times a letter/number and captures the first letter or Number. Finally the whole string was replaced with the character in the group index 1.
Replacement string:
\1
DEMO
<?php
$text = 'IIIII havvvvve a bigggg tesssssttttt tomorrow soooo iiii 2222551111 haveeee to do this rightttttt';
$pattern = '~([A-Za-z0-9])\1\1+~';
echo preg_replace($pattern,'\1',$text);
?>
Output:
I have a big test tomorrow so i 2551 have to do this right

([A-Za-z0-9])(\1{2,})?
Try this.Replace with $1.
See demo..
http://regex101.com/r/sA7pZ0/27

Related

PHP Regex to get text between 2 words with numbers

i'm trying to get the string between two words in a entire string:
Ex.:
My string:
...'Total a Facturar 123,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado'...
I'm using
/(?<=Total a Facturar )(.*?) Recepcionado/
I need the highlighted characters (26,860161,16080,580310,760)
and i get 221,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado with my pattern.
The numbers of the string are always different, i need the numbers that are together without a space.
Thanks
EDIT:
Here is the entire string: eval.in/802292
I hope this will be helpful
Regex demo or Regex demo 2
Regex: (?:\d+(?:\,\d+){2,})
For above question you can also use it like this (?:\d+(?:\,\d+){4})
1. (?:\d+) this will match digits one or more.
2. (?:\,\d+){2,} Adding this in expression will match patterns like , and digits {2,} for 2 or more than 2 times.
PHP code: Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = "Total a Facturar 123,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado";
preg_match("#(?:\d+(?:\,\d+){2,})#", $string, $matches);
print_r($matches);

Regex to find digits between the last set of parentheses

i was struggling to make a regex which extract the digits bettween the last set of parentheses from a string, and this is what i came with until now:
^.*?\([^\d]*(\d+)[^\d]*\).*$
E.g.:
This is, (123456) a string (78910);
It returns 123456 , which is great but i need it to look at the last set and return 78910.Also , i want the regex to ignore everything but digits:
This is, (123bleah456) a string (789da10);
Should return: 78910
UPDATE
Using regex:
(\d+)(?!.*\d)
For string:
Telefon Mobil (123)Apple iPhone 6 128GB Gold(1567)asd234
Will return 234 when it should be 1567
Rubular
You can extract the last one by use of greed:
.*\(\K\d+
See demo at regex101
\K resets beginning of the reported match.
For your more specific updated case slightly modifiy the regex and strip out non-digits.
$str = "This is, (123bleah456) a string (789da10);";
if(preg_match('/.*\(\K\d[^)]*/', $str, $out))
$res = preg_replace('/\D+/', "", $out[0]);
See demo at eval.in

php regex to match text

I need a php regex to match text that is not preceded by the name "Total" of "maximum" case insensitive in the text below.
[1]
[1m]
[1mk][1mks]
[1mark]
[1marks]
(1mk)
12mk
12 mark
13 mark
[Total: 15]
Total: 16 mark
Total 1 mark
Total 12 mark
Total: 9 mark
Total: 10 mark
[Total: 11 marks] Total 6 mark
maximum 5 marks
maximum:5 marks
Note: This text is in a one long line.
The regex should match the following
[1]
[1m]
[1mk][1mks]
[1mark]
[1marks]
(1mk)
12mk
12 mark
13 mark
I have tried this one but its not working
/(?<!Total\:\s|Total\s|maximum\s|maximum\:\s)[\[|\(]?([0-9]{1,2})(\s|(?=marks|mark|mks|mk|m|\]))?(\]|marks|mark|mks|mk|m)[\]|\)]?/i
EDIT
https://www.debuggex.com/r/yNNN_B3iQmGyYWoz
EDIT2
e.g '12 mark' should be returned only is its not "Total[:]\s+ 12 mark" or "maximum[:]\s+12 mark"
Try this: (?:\[?\b(?:Total|maximum):?\s?\d+\s?[^ ]+(*SKIP)(*FAIL))|(\d++\s?[^ )\]]*)
(Use ignore case.)
Explanation
Part 1
(?:\[? Non capturing group that may have a [
\b Boundary
(?:Total|maximum) non capturing group matching either literal
:?\s?\d+\s? Maybe a : maybe a space, some digits, maybe another space.
[^ ]+ A bunch of non spaces.
(*SKIP)(*FAIL))| Plot twist: Anything matching Part 1 FAILS
Part 2
This is captured, for real.
\d++\s? digits, maybe followed by a space.
[^ )\]]* And maybe stuff that's not a space, ), or ].
The PHP should look something like this:
preg_match_all(
'/(?:\[?\b(?:Total|maximum):?\s?\d+\s?[^ ]+(*SKIP)(*FAIL))|(\d++\s?[^ )\]]*)/i',
"YOUR STRING",
$matches
);
print_r($matches[0]);
Actually I would go for the two step solution. First clean up the trashy words by replacing them with this regexp:
(Total:?\s?|maximum:?\s?)
Then match all the content you really need is easy:
\[?\(?([0-9]{1,2}\s?marks?|[0-9]{1,2}\s?mk?s?)\)?\]?
No idea how to use debuggex.com but I tested all regular expressions in pspad so it definitely works.

php preg_replace frustration

Im reluctant to ask but I cant figure out php preg_replace and ignore certain bits of the sting.
$string = '2012042410000102';
$string needs to look like _0424_102
The showing numbers are variable always changing and 2012 changes ever year
what I've tried:
^\d{4}[^\d{4}]10000[^\d{3}]$
^\d{4}[^\d]{4}10000[^\d]{3}$
Any help would be appreciated. I know it's a noob question but easy points for whoever helps.
Thanks
Your first regex is looking for:
The start of the string
Four digits (the year)
Any single character that is not a digit nor { or }
The number 10000
Any single character that is not a digit nor { or }
The end of the string
Your second regex is looking for:
The start of the string
Four digits (the year)
Any four characters that are not digits
The number 10000
Any three characters that are not digits
The end of the string
The regex you're looking for is:
^\d{4}(\d{4})10000(\d{3})$
And the replacement should be:
_$1_$2
This regex looks for:
The start of the string
Four digits (the year)
Capture four digits (the month and day)
The number 10000
Capture three digits (the 102 at the end in your example)
The end of the string
Try the following:
^\d{4}|10000(?=\d{3}$)
This will match either the first four digits in a string, or the string '10000' if there are three digits after '10000' before the end of the string.
You would use it like this:
preg_replace('/^\d{4}|10000(?=\d{3}$)/', '_', $string);
http://codepad.org/itTgEGo4
Just use simple string functions:
$string = '2012042410000102';
$new = '_'.str_replace('10000', '_', substr($string, 4));
http://codepad.org/elRSlCIP
If they're always in the same character locations, regular expressions seem unnecessary. You could use substrings to get the parts you want, like
sprintf('_%s_%s', substr($string,4,4), substr($string,13))
or
'_' . substr($string,4,4) . '_' . substr($string,13)

How to retrieve data after the 1st occurence of a number/digit in a string in php

We have a query(mysql) which returns data in the following format of alphabets followed by digits followed by alphabets like --
text1 12.12 mg text2
Now the issue is , i need to write a script in php which gives all the data starting from the 1st digit.so the result should be something like
12.12 mg text2
I am not sure how to accomplish this in php and the functions which might be of use for this purpose.
Any help would be appreciated.
preg_match('#\\d+(.*)$#', $message, $match);
$text = $match[1];
the \\d+ means one or more consecutive digits. (.*) means match any character except a new line. $ tells it to go to the end of the string...

Categories