Jump to content

preg_replace issue


GD77

Recommended Posts

Hello:

I'm using the following to format a number:

$ne=1113444555

$v1=substr($ne, 0,3)."-".substr($ne, 3,1)."-".substr($ne, 4,3)."-".substr($ne, 7);

result: 111-3-444-555

 

Trying with preg_replace... not working well since I can find the match but not successful in converting it:

$v1=preg_replace('/(([(\d]{3}){1}([(\d)]{1}){1}([(\d)]{3}){2})/', '$1-$2-$3-$4', $ne);

Link to comment
https://forums.phpfreaks.com/topic/265890-preg_replace-issue/
Share on other sites

fixed with:

'~([(\d]{3}){1}([(\d)]{1}){1}([(\d)]{3}){1}([(\d)]{3}){1}$~', '$1-$2-$3-$4',....

but I m using ([(\d)]{3}){1} twice should not that be replaced with ([(\d)]{3}){2}? :/

anyway here how I ve fixed it http://www.functions-online.com/preg_replace.html online test

 

still don t egt it why ~ delimiter is obligatory in it :/

Link to comment
https://forums.phpfreaks.com/topic/265890-preg_replace-issue/#findComment-1362388
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.