afeef afeef - 4 months ago 13
PHP Question

How to make first letter after hypen in string to be capital

If I have string like

hybert-metal
then I need the output to be
Hybert-Metal


I tried these scripts:

<?php
$form['surname']->render(array('onChange'=>"javascript:this.value = this.value.toLowerCase()", 'onBlur'=>"javascript:this.value=this.value.replace(/\w\S*/g, function(txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()})"));
?>


<input type="text" name="surname" onchange="javascript: this.value = this.value.replace(/\w\S*/g, function(txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toUpperCase() })" onblur="javascript: this.value = this.value.replace(/\w\S*/g, function(txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() })" id="surname">


On trying script this makes
Hybert
but make string
metal
stays as its is. The output after my script is
Hybert-metal
but I need
Hybert-Metal
.

The problem is that I cannot make string first text capital. Any suggestion is most welcome.

Answer

There's no need for JS or PHP code here as you can do this in CSS alone using text-transform: capitalize:

span {
    text-transform: capitalize;
}
<span>hybert-metal</span>

i need to type on textbox so onchange event would fire css trick don"t works

Yes it does, as it works for input elements too:

.capitalise {
  text-transform: capitalize;
}
<input type="text" class="capitalise" value="hybert-metal" />