Hi, I wonder if someone can help me out, I need to detect and remove any group of 5 or more numbers matching the conditions below using one or multiple regular expressions. If someone could post a solution in the forum that would be great or if it’s a big job contact my via PM and I can pay you for your time. Thanks in advance for any help you can give. 0123456789 0 1 2 3 4 5 6 7 8 9 for one or multiple spaces 0-1-2-3-4-5-6-7-8-9 Or any of the following in between the numbers !â€Â£$%^&*()_+<>;’,./\|`¬ zero one two three four five six seven eight nine for one or more spaces Or any of the following in between the numbers !â€Â£$%^&*()_+<>;’,./\|`¬ Eg. zero – one – two – three – four – five – six – seven – eight – nine zero : one : two : three : four : five : six : seven : eight : nine Or combination of the above such as 0 one two three 4 five 6 seven 8 nine or 0 - 1 - two - three - 4 - five - 6 - seven - 8- nine Update to clarify what I’m looking for. I need the detected group of 5 or more numbers to be replaced by **number removed**
$z="something else 0 1 - two - three - 4 five - 6 - seven - 8- nine something else "; $z=preg_replace('#(zero|one|two|three|four|five|six|seven|eight|nine)#','',$z); $z=preg_replace('#\d[\d\s-]{2,}#','',$z); echo $z;
Hi patlatyj Thanks for your post however it’s not what I was looking for. It’s my fault for not being more specific. I need the detected group of 5 for more number to be replaced by **number removed** So something else 0 1 - two - three - 4 five - 6 - seven - 8- nine something else Would become something else **number removed** something else
**number removed** - a number of digits removed or literally text "**number removed**"? if latter $z=preg_replace('#([\d\s-]|zero|one|two|three|four|five|six|seven|eight|nine){2,}#',' **number removed** ',$z);
Yes that right, I want to replace the numbers detected with **number removed** I tried the expression you posted but doesn’t cover all the conditions I need. My knowledge of regular expressions is very limited but I’ve made some modifications to the ones you’ve posted and come up with the one below: $z = preg_replace( '/((zero|one|two|three|four|five|six|seven|eight|nine|\d)[(zero|one|two|three|four|five|six|seven|eight|nine|\d)\s-!:£$%^&*()_+{}\[\]~#\|,–.<>\/\\\]){5,}/i',' **Removed** ', $z); This detects the following variations: 0123456789 0 1 2 3 4 5 6 7 8 9 0-1-2-3-4-5-6-7-8-9 1:2:3:4:5:6:7:8:9:0 0-1-2(3)4-5-6-7(8)9 zero one two three four five six seven eight nine for one zero–one–two–three–four–five–six–seven–eight–nine zero:one:two:three:four:five:six:seven:eight:nine 0 one two three 4 five 6 seven 8 nine etc Code (markup): However it doesn’t cover numbers with more than a single instance of !:£$%^&*()_+{}\[\]~#\|,–.<>\/ between them 0 1 2 3 4 5 6 7 8 9 two or more spaces zero – one – two – three – four – five – six – seven – eight – nine zero : one : two : three : four : five : six : seven : eight : nine zero : one : two : three : four : five : six : seven : eight : nine zero::one::two::three::four::five::six::seven::eight::nine 0 - 1 - two - three - 4 - five - 6 - seven - 8- nine Code (markup): It detects numbers with a single space,dot,dash etc but not multiple spaces,dots,dashes etc Can the regular expressed be modified to also include the above? Thanks agian for any help
Ok just a quick update, I’ve created the following regular expression which seems to detect all the variations I’ve motioned in the thread. I still need to do some more testing with it to ensure it doesn’t match anything by mistake. $z = preg_replace( '/((zero|one|two|three|four|five|six|seven|eight|nine|\d)+|[\s-!:£$%^&*()_+{}\[\]~#\|,–.<>\/\\\]+(zero|one|two|three|four|five|six|seven|eight|nine|\d)){5,}/i',' **Removed** ', $z); I would appreciate if a regular expression expert could take a look and see if I’ve made any mistakes that could cause a problem. Thanks
Does this do it for you ? <?php $a[] = 'three – four – five – six – seven – eight – nine'; $a[] = '...: one : two : three : four : five : six : seven : eight :...'; $a[] = 'two : three : four : five damnit mizzan'; $a[] = 'zero::one::two::three::four::five::six::seven::eight::nine'; $a[] = '0 - 1 - two - three - 4 - five - 6 - seven - 8- nine'; foreach($a as $aa) { echo preg_replace('#((zero|one|two|three|four|five|six|seven|eight|nine|\d+)[^a-z0-9]+){4,}(zero|one|two|three|four|five|six|seven|eight|nine|\d+)#i', '**Number Removed**', $aa) . '<br/>'; } ?> PHP:
Thanks joebert, that’s almost perfect Just one small problem, it detects everything except the following: 436333 4-1-43633 Would it be possible to modify your expression to cover the above or would it be better to use another regular expression to catch the ones that are missed?
This might do it. <?php $a[] = 'three – four – five – six – seven – eight – nine'; $a[] = '...: one : two : three : four : five : six : seven : eight :...'; $a[] = 'two : three : four : five damnit mizzan'; $a[] = 'zero::one::two::three::four::five::six::seven::eight::nine'; $a[] = '0 - 1 - two - three - 4 - five - 6 - seven - 8- nine'; $a[] = '436333'; $a[] = '4-1-43633'; foreach($a as $aa) { echo preg_replace('#((zero|one|two|three|four|five|six|seven|eight|nine|\d)[^a-z0-9]*){4,}(zero|one|two|three|four|five|six|seven|eight|nine|\d)#i', '**Number Removed**', $aa) . '<br/>'; } ?> PHP: