Capitalize first letter in <em>
-
12-06-2021 - |
Question
$item = 'some <html> goes <div class="here"> and </div> can be placed <em> some words</em>, and then more </html> can exist';
How do I capitalize the first letter of the first word inside <em>...</em>
only?
Solution
this?
function ucfirstword($str) {
$estr = explode(" ",$str);
$estr[0] = ucfirst($estr[0]);
return implode(" ",$estr);
}
...
echo "<em> ".ucfirstword("some words")."</em>";
OTHER TIPS
You can use a regular expression, like this:
function replaceStuff($matches){
return $matches[1].strtoupper($matches[2]);
}
$item=preg_replace_callback("/(<em[^>]*>[^\\w]*)(\\w)/i","replaceStuff",$item);
↪ Read more about using preg_replace_callback.
Note: you can also do this using CSS; to do this, you can use this code:
em:first-letter {
text-transform:capitalize;
}
if you need only this string, you can do like this
$item = 'some <html> goes <div class="here"> and </div> can be placed <em> ';
$item .= ucfirst("some words");
$item .= '</em>, and then more </html> can exist';
echo $item;
Try this:
$item = 'some <html> goes <div class="here"> and </div> can be placed <em style="text-transform: uppercase">some</em>, and then more </html> can exist';
Maybe this helps you.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow