Alphy Gacheru
Alphy Gacheru

Reputation: 657

How to capitalize a word that has all letters in as capital letters in css OR php

I have the following code and using css text-transform property with a value of capitalize or php ucwords() I get the title capitalized but when the title is provided with all letters as capitals it doesn't work. How can I achieve the output to be capitalized regardless of the title provided?

  <h4 style="text-transform: capitalize;">
      <a href="{{ route('posts.show', $post )}}">{{ $post->title }}</a>
  </h4>

Upvotes: 0

Views: 340

Answers (3)

user1207013
user1207013

Reputation: 38

Twig has as capitalize filter

https://twig.symfony.com/doc/3.x/filters/capitalize.html

{{ 'my first car'|capitalize }}

{# outputs 'My first car' #}

Perfect spot to do this, the view that is.

Upvotes: 1

endeavour
endeavour

Reputation: 619

A php solution would be to convert case to lower and then use ucfirst

$text = 'THIS IS A TITLE';
$normalText = ucfirst(strtolower($text));
echo $normalText;

Upvotes: 0

dave
dave

Reputation: 2911

As long as it's only one line you can do it with CSS by combining lowercase and capitalize using the first-line pseudo selector:

.title-case {
  text-transform: lowercase;
  display: inline-block;
}

.title-case::first-line {
  text-transform: capitalize;
}
<h4>
  <a class="title-case" href="#">SHOUTING</a>
</h4>

Upvotes: 2

Related Questions