By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

Regular Expression Tags To Lower & Adding quotes

P: 4
Hello I'm Using The Following Code To Do 2 things
1-Convert Tags To Lower Case
2-Add Quotes To Attributes That Doesn't Have Quote but i'm having problems with some attributes.

The Functions I'm using :
[PHP]// convert tags to lowercase
$html = preg_replace( "/(<\/?)(\w+)([^>]*>)/e", "'\\1'.strtolower('\\2').'\\3'", $html);
// close br, hr and img tags
$html = preg_replace( array('/<(br)>/', '/<(hr\s?.*?)>/', '/<(img\s.*?)>/'), '<\\1 />', stripslashes($html) );
// add quotes for href tags that don't have them
$html = preg_replace( '|([a-z]=)([^"\'][^\s>"\']+)["\']?|', '$1"$2"', $html );[/PHP]

My Original HTML is :

[HTML]<P align=justify><FONT size=2><FONT face=Verdana><STRONG>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;</STRONG><STRONG>Egypt</STRONG> has witnessed an astonishing growth in the real estate industry similar to that of the West. As the cities get over crowded with the population, urban sprawl reaches to its highest growth Egypt has ever witnessed. <BR>&nbsp;&nbsp;&nbsp; <STRONG>Many </STRONG>new cities are being built outside of Cairo(the Capital) to accommodate the ever increasing population growth. It is likely that eventually these cities will all join as the demand increases.<BR>&nbsp;&nbsp;&nbsp; <STRONG>Some</STRONG> examples are New Cairo which includes town like, Tajammu 1st, 3rd and 5th.&nbsp; Prices of&nbsp; Lands and Apartments for sale are on the rise monthly with no slow down in the horizon. In 5th Tajammu, one may find the most expensive homes (villas) in Egypt. Some homes have sold as high as 80 million USD in this area. Buildings are being erected every where and the properties for sale are in high demand.<BR>&nbsp;&nbsp;&nbsp; <STRONG>6th October City</STRONG> is also another new sprawl growth city accommodating much of the Egyptian population as well as TV and Satellite industries. <BR>&nbsp;&nbsp;&nbsp; <STRONG>The 6th of October</STRONG> city-based Media Production City (<STRONG>MPC</STRONG>) covers an area of about 35 million square meters, and is the biggest ever built information and media complex, which, together with the Egyptian satellite "Nile Sat", will allow Egypt to compete with many Middle Eastern countries in this field. <BR>One may find flats (apartments) for sale in Egypt as low as a few thousand dollars and as high as millions. It all is the matter of supply and demand and location.</FONT></FONT></P>[/HTML]

the Problem Is someting like :
Expand|Select|Wrap|Line Numbers
  1.  <FONT size=2> 
doesn't Change to
Expand|Select|Wrap|Line Numbers
  1. <font size="2"> 
but it's changed to
Expand|Select|Wrap|Line Numbers
  1. <font size=2>
May 30 '07 #1
Share this Question
Share on Google+
4 Replies


pbmods
Expert 5K+
P: 5,821
Heya, shehabic. Welcome to TSDN!

the Problem Is someting like :
Expand|Select|Wrap|Line Numbers
  1.  <FONT size=2> 
doesn't Change to
Expand|Select|Wrap|Line Numbers
  1. <font size="2"> 
but it's changed to
Expand|Select|Wrap|Line Numbers
  1. <font size=2>
I'm working on a similar problem, and my advice to you is to try explodeing the string by '=' characters. Then append a '"' to the end of each piece except the last one (unless it already ends with a '"'), and prepend a '"' to each piece except the first one (unless it already starts with a '"'). Finally, implode the array, using '=' to join it back together.
May 30 '07 #2

P: 4
No No
i don't wanna do it in this way , I Did what I Wanted Btw by this :

add this line :

[PHP]$html = preg_replace( '|([a-z]=)([0-9]+)|', '$1"$2"', $html );[/PHP]

but i still have a problem in this :

[PHP]$html = preg_replace( array('/<(br)>/', '/<(hr\s?.*?)>/','/<(img\s?.*?)>/'), '<\\1 />', stripslashes(stripslashes($html)) );[/PHP]

it makes a / /> when the tag already contain /> At its end e.g :
[HTML]<img src='anything' /> will be <img src='anything' / />[/HTML]

I Want It To Add /> Only if the tag <img doesn't contain that . .
May 31 '07 #3

pbmods
Expert 5K+
P: 5,821
[PHP]$html = preg_replace( array('/<(br)>/', '/<(hr\s?.*?)>/','/<(img\s?.*?)>/'), '<\\1 />', stripslashes(stripslashes($html)) );[/PHP]
.* matches the '/' character, too. You'll be wanting to use [^\/]* instead.
May 31 '07 #4

P: 4
.* matches the '/' character, too. You'll be wanting to use [^\/]* instead.
I Tried Many Combination To Do That But Failed To Write It In A Correct Way , Can You Rewrite it ?
May 31 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.