0

Im a bit stumped on how to make a string uppercase in php while not making the markup uppercase.

So for example:

<p>Chicken &amp; <a href="/cheese">cheese</a></p>

Will become

<p>CHICKEN &amp; <a href="/cheese">CHEESE</a></p>

Any advice appreciated, thanks!

cadaa
  • 192
  • 1
  • 3
  • 7
  • 4
    Have you ever thought of using CSS? – Gumbo Oct 27 '10 at 09:04
  • CSS is a good option as long as you don't try to uppercase the content of an input field. this totally fails because it is shown as uppercase but still remains lowercase and so will be sent to your server as a lowercase string – ITroubs Oct 27 '10 at 09:06
  • FYI, Gumbo is talking about `text-transform: uppercase`. – Lekensteyn Oct 27 '10 at 09:06
  • @ITroubs, you should correct that on your server anyway. No client input can be trust. – Lekensteyn Oct 27 '10 at 09:07
  • @lekensteyn uppercase in input fields already was discussed here: http://stackoverflow.com/questions/3862215/css-text-transform-not-sending-upper-case-to-business-layer/3862265#3862265 that's why i wrote that ;-) – ITroubs Oct 27 '10 at 09:18
  • @cadaa could you please finally review the answers you have been given here and either accept one as the most helpful or provide additional information about why none of these solve your problem. thanks. – Gordon Oct 30 '10 at 17:18

4 Answers4

4

The following will replace all DOMText node data in the BODY with uppercase data:

$html = <<< HTML
<p>Chicken &amp; <a href="/cheese">cheese</a></p>
HTML;

$dom = new DOMDocument;
$dom->loadHTML($html);
$xPath = new DOMXPath($dom);
foreach($xPath->query('/html/body//text()') as $text) {
    $text->data = strtoupper($text->data);
}
echo $dom->saveXML($dom->documentElement);

gives:

<html><body><p>CHICKEN &amp; <a href="/cheese">CHEESE</a></p></body></html>

Also see

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
3

Well you could use the DOM class and transform all text with it.

EDIT: or you could use this css:

.text{
    text-transform: uppercase;
}

as GUMBO suggested

ITroubs
  • 11,094
  • 4
  • 27
  • 25
1

Parse it, then capitalize as you like.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • why parsing with a PHP written parser wen PHP itself has DOM XML functions since version 4 and a whole DOM class since PHP 5? – ITroubs Oct 27 '10 at 09:14
-1

I would be tempted to make the whole string uppercase...

$str = strtoupper('<p>Chicken &amp; <a href="/cheese">cheese</a></p>');

...And then use a preg_match() call to re-iterate over the HTML tags (presuming the HTML is valid) to lowercase the HTML tags and their attributes.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • And what if there already are some uppercase letters inside the tags? – Gumbo Oct 27 '10 at 09:08
  • 2
    This approach is very error-prone for complex HTML code. I wouldn't recommend it. – Lekensteyn Oct 27 '10 at 09:08
  • 1
    I doubt that this will work as you have to replace so many tags plus all those entities like `&` and `ü` and tag attributes like ``. – jantimon Oct 27 '10 at 09:09
  • apache also makes a big difference between a picture.jpg and a Picture.jpg and if you transform the Picture.jpg into a picture.jpg in a tag you will run into problems verry fast – ITroubs Oct 27 '10 at 09:11