Making code between < & > lower case?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Making code between < & > lower case?

Post by Mr Tech »

I got a WYSIWYG editor which puts most of the HTML code uppercase... Is there anyway to make everything betweend the < & > lower case and not effect the non HTML code?

Maybe even a HTML to XHTML convertor?

Thanks

Ben
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Code: Select all

preg_replace('/<(.+?)>/ies','strtolower("\\1")',$text);
I'm pretty sure that'll do it.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Yep, thanks. It did the job however I had to change it to: strtolower("<\\1>") otherwise it remvoed the < & >.

Also, is it possible to put quotes into html? For exaample:

Code: Select all

<tabel border=0 height=100 width=300 cellpadding=4 cellspacing=1>
Would change to:

Code: Select all

<tabel border=&quote;0&quote; height=&quote;100&quote; width=&quote;300&quote; cellpadding=&quote;4&quote; cellspacing=&quote;1&quote;>
Thanks for your help!
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Ahaha. Rather than change it to strtolower("<\\1>"), change the first bit to.. (plus I fixed it a bit)

Code: Select all

preg_replace('/(<[^>]+>)/ies','strtolower("\\1")',$text);
my bad.

Ok, so to put quotes in... hmm... Assuming the param value is alphanumeric, then this might do the trick:

Code: Select all

preg_match_all('/<[^>]+>/is',$text,$matches);
$out = array();
for ($i=0; $i < count($matches[0]); $i++) {
  $matches[0][$i] = preg_replace('/(\w+)=(\w+)/is','\\1="\\2"',$matches[0][$i]);
}
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Awesome works well... Thanks for your help on this. I really apprciate it :) Just one hiccup though...

1. One tags such as alt=This is a picture it turns it into alt="This" is a picture

Hopefully this isn't to complicated...

Also how do I set it up so that it keeps the non html code in the text? I tried:

Code: Select all

preg_match_all('/<&#1111;^>]+>/is',$xhtml,$matches);
$out = array();
for ($i=0; $i < count($matches&#1111;0]); $i++) {
  $matches&#1111;0]&#1111;$i] = preg_replace('/(\w+)=(\w+)/is','\\1=&quote;\\2&quote;',$matches&#1111;0]&#1111;$i]);
  $final .= $matches&#1111;0]&#1111;$i];
}
But it removed all the non html text.

Thanks again :)
Last edited by Mr Tech on Wed May 18, 2005 11:30 pm, edited 1 time in total.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Never mind about the alt tag... It seems the WYSIWYG edit (which is stuffing up my code) adds quotes around them when the words are anymore then one word long...

All I need to know now is how to get it to show the non html text too...
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Silly me... I worked it out....

This is the code that ended up working

Code: Select all

preg_match_all('/<&#1111;^>]+>/is',$text,$matches);
$out = array();
for ($i=0; $i < count($matches&#1111;0]); $i++) {
  $text .= preg_replace('/(\w+)=(\w+)/is','\\1=&quote;\\2&quote;',$matches&#1111;0]&#1111;$i]);
}
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

ARGHHH!!! For some reaosn the code is no longer working... Any ideas?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

It might be easier to search around for option that will make it write tags in lowercase.
Hmmm... i have a premonition.. are you using MS-Frontpage :?: then, buddy, there is the option to make it write tags in lowercase :wink:
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Nah an online WYSIWYG editor called htmlarea... It's no longer developed and I can't find any options to make it lower case or put quotes around code in the source code... This script was working before but has all of a sudden stopped working... I might have chnaged the code without knowing...
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

you could make your life easier and check out some of the many editors that have been discussed here
viewtopic.php?t=6288&postdays=0&postorder=asc&start=0
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

I'm unable to switch editors for various reasons... I think I worked it out though... It seems stable enough...

Instead of searching for everything between the < & >, I just used this code:

Code: Select all

$xhtml =preg_replace('/(\w+)=(\w+)/is','\\1="\\2"',$xhtml);
The only problem is if someone enters something like red=color into the WYSIWYG editor it will put quotes around it but that's ok. I doubt anyone would need to do that...

Let me know if you think this code may not work for some cases...
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

My advice is download htmltrim from http://www.w3.org and us it to tidy up your old code. htmltidy is another good piece of free software for tidying up your code.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

:arrow: Moved to regex
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

The code does not like something like

Code: Select all

&lt;a href=&quote;http://www.google.com&quote;&gt;
The colen (:) seems to stuff it up...

Any ideas on how to fix this? This is the code:

Code: Select all

$xhtml =preg_replace('/(\w+)=(\w+)/is','\\1="\\2"',$xhtml);
Post Reply