0

In my Website i want to use the german letter ß, which only exists as a lowercase letter. Though i want all my headlines to be shown in uppercase letters (capitalize), i use font-transform: uppercase. But that replaces the ß letter with a uppercase SS, which is not wanted due to certain design and programming features.

So i was thinking about a preg_replace_callback, that changes all letters in the headlines in my HTML code to their uppercase letters.

$content = preg_replace_callback('/(<h\d>)(.*)(<\/h\d>)/', function($matches) {
    return $matches[1] . mb_strtoupper($matches[2], 'UTF-8') . $matches[3];
}, $content);

That works well on headings with no special attributes or nested links etc. As i'm a total newbie in regular expressions i don't know how to write those.

Basically <h2 class="heading-italic"><a href="http://www.google.de">Google</a></h2>should become to <h2 class="heading-italic"><a href="http://www.google.de">GOOGLE</a></h2>

How would my regular expression look, that is also catching headings with attributes and also (multiple) nested elements?

chris.ribal
  • 435
  • 2
  • 10
  • I'd like to recommend two things: 1) Stick with the double S for capitalized ß, as this is widely accepted to be "correct". There's a reason CSS does it this way. It's like mixing minus/dash (-/–), apostrophe/accent, [plenken](https://en.wikipedia.org/wiki/Plenken) and so on: The user understands it, yet it's wrong. 2) [Do not parse HTML with regex](http://stackoverflow.com/a/1732454/3890673) :-) – crusy Feb 23 '17 at 07:00

1 Answers1

0

Change /(<h\d>)(.*)(<\/h\d>)/ to this one:

 $newregex = '/(<h\d.*?>)(<a .*?>)?(.*)(<\/a>)?(<\/h\d>)/' ;

 $content = preg_replace_callback( $newregex , function($matches) {
     return $matches[1] . mb_strtoupper($matches[3], 'UTF-8') . $matches[5];
 }, $content);

Note mb_strtoupper($matches[3], 'UTF-8') and [3] is used for new regex and 3rd group. group2 and group4 are optional.

MohaMad
  • 2,575
  • 2
  • 14
  • 26