Matt
Matt

Reputation: 1149

Can I "fix" misplaced HTML tags using PHP?

Is there a quick and easy way to fix HTML tags that are misplaced, in a web document? Such as:

<strong><span style="border:1px;">Text</strong></span>
   /\                                    /\
   |______________________________________|

So that it looks like:

<strong><span style="border:1px;">Text</span></strong>

Edit: you are suggesting HTML fixers, but what I'm looking for is a function type solution. Would it help if you could consider this to be BBcode? [b][u]Text[\b][\u]

Upvotes: 0

Views: 102

Answers (3)

Aziz Saleh
Aziz Saleh

Reputation: 2707

I think the best solution is using Html Purifier, works pretty good:

Demo: http://htmlpurifier.org/demo.php

Works with your input perfectly.

Upvotes: 1

Gergo Erdosi
Gergo Erdosi

Reputation: 42048

You can use tidy::repairFile() or tidy::repairString(), but repairing is not straightforward, so you can never be sure the result will be what you expect. Example from the documentation:

<?php
$file = 'file.html';

$tidy = new tidy();
$repaired = $tidy->repairfile($file);
rename($file, $file . '.bak');

file_put_contents($file, $repaired);
?>

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

How should a computer know whether you meant for the span to be inside the strong, or the other way around?

The "quick and easy way" is to run your document through an HTML validator, then fix the issues that it identifies using your noggin and keyboard.

Upvotes: 0

Related Questions