Reputation: 511
I want to remove everything from a string using regular expression except alpha and numeric characters and I need any leading zero removed.
The below works but does not remove leading zeros
$string = '00000000A1234567890-=qwesss €#¢∞§¶¶•ªº– ≠≠rtyuuiop[]\';lkjhgfdsazxcvbnm,./';
$pattern = '/([^\da-z]/i)';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
How can i alter the pattern to remove leading Zeros?
Upvotes: 0
Views: 990
Reputation: 785008
This should work to satisfy both requirements:
$string = '00000000A1234567890-=qwesss €#¢∞§¶¶•ªº– ≠≠rtyuuiop[]\';lkjhgfdsazxcvbnm,./';
$pattern = '/^0+|[^\dA-Za-z]+/';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
//=> 1234567890qwesssrtyuuioplkjhgfdsazxcvbnm
Upvotes: 4
Reputation: 111829
This one will work:
<?php
$string = '00000000A1234567890-=qwesss €#¢∞§¶¶•ªº– ≠≠rtyuuiop[]\';lkjhgfdsazxcvb00000nm,./';
$pattern = '#^(0*)|([^\da-z])#i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
EDIT
This pattern can also be simplified to:
$pattern = '#^0+|\W#';
Upvotes: 0
Reputation: 139
Try it:
$string = '00000000A1234567890-=qwesss €#¢∞§¶¶•ªº– ≠≠rtyuuiop[]\';lkjhgfdsazxcvbnm,./';
$pattern = '/([\W])/i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
And if you also want delete zeros pattern is:
$pattern = '/([\W0])/i';
Upvotes: 0