RE: [PHP] Ereg capitalization
| From: | John W. Holmes | Date: | Wed, 09 Apr 2003 03:33:22 +0000 |
| Subject: | RE: [PHP] Ereg capitalization | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-142993@lists.php.net to get a copy of this message | ||
> I'm not too great with Ereg, so I'm going to pass this along here.
>
> Is there a way I can make all of my tags uppercase using ereg? Well, I
> know there is, but how. I need to make it so it's something like
>
> "<[A-Z]+["]*>
>
> Something like that. The criteria is that all tags must be in
uppercase,
> but everything inside the quotation marks is to be in lowercase. Any
> ideas?
I really tried to come up with a regex for this, but I couldn't. So here
is a script that'll just loop through each letter of a string and
uppercase the letters that are within the tags but not within quotes
(single or double).
Adapt to your needs.
<?php
$str = 'This is <img src=\'foo.jpg\' width="50" height="40"
bad>
a <b>thing</b>, okay? <a href="foo.html">Link</a> But
4 > 2 and 4 <= 4, okay? <yeah>';
$length = strlen($str);
$within_quotes = 0;
$within_tag = 0;
for($x=0;$x<$length;$x++)
{
if(!$within_tag)
{
if($str{$x} == '<' && preg_match('![/a-zA-Z]!',$str{$x+1}))
{
$within_tag = 1;
$within_quotes = 0;
}
}
elseif($within_tag)
{
if($str{$x} == '"' || $str{$x} == "'")
{ $within_quotes = !$within_quotes; }
elseif($str{$x} == '>' && !$within_quotes)
{ $within_tag = 0; }
}
if($within_tag && !$within_quotes)
{ $str{$x} = strtoupper($str{$x}); }
}
echo "<pre>",htmlentities($str),"</pre>";
?>
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/