Jump to content

Custom HTML validation when inserting using PHP


matfish

Recommended Posts

Hi,

 

I have a javascript "MS-WORD" like script to allow users to add content to a page, this then gets inserted into the database. Cut a long story short - for some reason it just won't let me validate HTML correctly therefore I just need to know:

 

When inserting into the database I need to do a find for <img> tags only and add a slash before the closing bracket. For example find: <img src="image.jpg" border="0"> and add <img src="image" border="0" />

I do not want to do just a find and replace on all brackets as its not needed in some cases. Ideally it needs to just look at the <img> tags and add that last slash.

 

Any ideas? Many thanks

Link to comment
Share on other sites

I have a javascript "MS-WORD" like script

In other words you have a HTML text editor built in Javascript such as http://www.fckeditor.net/

You really should not have to do any replacing on elements. With these text editors there are usually configuration files that set the formatting of the document. You should be able to set XHTML as the preferred standard. If not get a better editor like the one posted above. It is fully configurable.

Link to comment
Share on other sites

Hi,thanks for the reply.

 

Yes I have something similar however on this project I'm going to have to leave the current one in place as it was bespoke build with Ajax and one-click editing. When we have more time I will definitely be updating to the one you mentioned - looks great!

 

In the meantime, I just need to alter the <img> tag as mentioned previously when the html is submitted to the database.

 

 

Link to comment
Share on other sites

There are many non-paired tags that would need re-formatting however you can do this in one fowl swoop but you will need the 'php html tidy' extension. Read docs to install i.e linux: yum install php-tidy

 

Convert the string to xhtml using

<?php
$string = "<img src='test.gif'><br><img src='test.gif'>";

$tidyConfig = array('output-xhtml' => true);
$string = tidy_parse_string($string, $tidyConfig, 'UTF8');
// display the new output
print "<xmp>".$string."</xmp>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.