Copy link to clipboard
Copied
Hello,
Am still newbie in coldfusion.
I have a telephone number of 12 digits, for e.g, +331234567890.
Now I have to replace the country code by 0,i.e, +33 to 0. I have already done that.
My problem is how to separate these 10 remaining numbers and put a dot in between each 2 numbers like 12.34.56.78.90 and at the end join the country code like 0.12.34.56.78.90
I was thinking of doing a Len() of the numbers and then separate them by a dot but unfortunately I don't know how to do it.
Can anybody help me please ?
Thanks in advance.
<cfset dotted_reverse_telno = "">
<cfset telno = "01234567890">
<!---Reverse the tel number --->
<cfset reverse_telno = reverse(telno)>
<!---Run through the digits in the reversed number, adding a dot after every 2 digits --->
<cfloop from="1" to="#len(reverse_telno)#" index="n">
<cfif n mod 2 EQ 0>
<cfset dotted_reverse_telno = dotted_reverse_telno & mid(reverse_telno, n, 1) & "." >
<cfelse>
<cfset dotted_reverse_telno = dotted_reverse_telno & mid(reverse_telno, n, 1)>
</cfif>
...Copy link to clipboard
Copied
<cfset dotted_reverse_telno = "">
<cfset telno = "01234567890">
<!---Reverse the tel number --->
<cfset reverse_telno = reverse(telno)>
<!---Run through the digits in the reversed number, adding a dot after every 2 digits --->
<cfloop from="1" to="#len(reverse_telno)#" index="n">
<cfif n mod 2 EQ 0>
<cfset dotted_reverse_telno = dotted_reverse_telno & mid(reverse_telno, n, 1) & "." >
<cfelse>
<cfset dotted_reverse_telno = dotted_reverse_telno & mid(reverse_telno, n, 1)>
</cfif>
</cfloop>
<!---Reverse the result --->
<cfset dotted_telno = reverse(dotted_reverse_telno)>
<cfoutput>#dotted_telno#</cfoutput>
Copy link to clipboard
Copied
Regex FTW.
<cfset newstr = reReplace(reverse("012456994088"), '([0-9]{2})', '\1.', 'all') />
<cfoutput>#reverse(newstr)#</cfoutput>
Not perfect as it has a dot at the start if it's an even number and not if it's odd, but that's easily fixed.
Copy link to clipboard
Copied
Thanks dude.
I did not know that we could make use of regex in coldfusion. Cool..
Yeps this could be easily fixed with the dot at start. Thanks a lot again for the quick reply.
Copy link to clipboard
Copied
Well I have already done half of the code u posted, it's just that I don't know how to separate and put the dot in middle.
Thanks a lot. It helps me loads....