Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to insert dash after three characters in a string?

Tags:

html

php

I have a form in the input=text the user enters a number like 123456 i want to change this to 123-456

How can I put a dash after a certain numbers of characters?

I tried many functions but all of them wants to change or relapse a character and i don't want to change any thing i just want to insert a dash - after three characters in the string.

thank you.

this is the code that takes the number from the user i want to put this number in a function that insert the dash after three character in the sting inserted buy the user

<tr>
 <td> <small>RIG CHARGE:</small> </td>
 <td> <input type="text" name="rch" value="<?php echo $objWorksheet->getCellByColumnAndRow(6, 21)->getValue(); ?>"> </td>
</tr>
like image 776
Haitham El Amashety Avatar asked Aug 13 '13 11:08

Haitham El Amashety


People also ask

How do I find a dash in a string?

You can check the count of dashes in a string with: if str. Count(x => x == '-') !=

How to add Hyphen in string in JavaScript?

To insert hyphens into a JavaScript string, we can use the JavaScript string's replace method. We call phone. replace with a regex that has 3 capturing groups for capturing 2 groups of 3 digits and the remaining digits respectively. Then we put dashes in between each group with '$1-$2-$3' .


1 Answers

$string = "123456";
$insertion = "-";
$index = 3;
$result = substr_replace($string, $insertion, $index, 0);
echo $result;
like image 146
Deepu Avatar answered Oct 28 '22 09:10

Deepu