regex - Search and replace number in string with PHP preg_replace -


i've got format: 00-0000 , 0000-0000.

my code far:

<?php $string = '11-2222'; echo $string . "\n"; echo preg_replace('~(\d{2})[-](\d{4})~', "$1_$2", $string) . "\n"; echo preg_replace('~(\d{2})[-](\d{4})~', "$100$2", $string) . "\n"; 

the problem - 0's won't added (i guess preg_replace thinks i'm talking argument $100 , not $1)

how can working?

you try below.

echo preg_replace('~(?<=\d{2})-(?=\d{4})~', "00", $string) . "\n"; 

this replace hyphen 00. still make simple

or

preg_replace('~-(\d{2})~', "$1-", $string) 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -