Convert string to uppercase in PHP but not html markup
Im a bit stumped on how to make a string uppercase in php while not making the markup uppercase.
So for example:
<p>Chicken & <a href="/cheese">cheese</a></p>
Will become
<p>CHICKEN & <a href="/cheese">CHEESE</a开发者_StackOverflow></p>
Any advice appreciated, thanks!
The following will replace all DOMText node data in the BODY with uppercase data:
$html = <<< HTML
<p>Chicken & <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 & <a href="/cheese">CHEESE</a></p></body></html>
Also see
- (related) Best Methods to parse HTML
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
Parse it, then capitalize as you like.
I would be tempted to make the whole string uppercase...
$str = strtoupper('<p>Chicken & <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.
精彩评论