PHP, fixing html tags in JavaScript.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nend
Forum Newbie
Posts: 2
Joined: Wed Aug 19, 2009 12:04 pm

PHP, fixing html tags in JavaScript.

Post by nend »

Ok everyone knows that </tag> is not valid in any JavaScript on a the page. It has to be <\/tag>. Only in the HTML can a closing tag be listed. So I am trying to get all my pages to XHTML standards, but I have some pesky JavaScript that is loading variables for certain things on my site. I can't add a second line of variables because they are set in other pages and don't want to change them because some are displayed outside of JavaScript tags. Plus it is a big rewrites so I came up with a simple solution I thought, but then I remember escaping things in PHP is my weak area. It just gets too confusing for me now. I read it up and still don't get it. I'm banging my head over here :banghead:

Ok I found this simple solution, all I have to do is edit the return contents if it is going to be in a part of JavaScript in the page.

Code: Select all

 
    return preg_replace('</','<\/', $content);
 
This is a simplified version of what I want to do, but only if that worked. :(

If I close the ending tag in the tags in the JavaScript it will be XHTML compatible.
nend
Forum Newbie
Posts: 2
Joined: Wed Aug 19, 2009 12:04 pm

Re: PHP, fixing html tags in JavaScript.

Post by nend »

Will some of my JavaScript has table, div, etc. tags in it. The closing tags miss up XHTML validation. The script in question is a news scroller.

But I figured it out. It was too simple, cant believe I didn't figure it out earlier.

So if I put {} it means exactly what is in between {}, right? I think some of my interpretations are wrong, I got to un-teach myself of these bad coding habits, lol.

My new code just in case someone else is having the same problems.

Code: Select all

 
    return preg_replace('{</}','<\/', $content);
 
It will replace a </tag> with <\/tag>, perfect for JavaScript.
Post Reply