Hello everybody.

Been trying to solve this for a couple of days now and just hitting a dead end. I have a WYSIWYG editor on a admin panel in replacement for a textarea box. This is all fine, except my client wants valid XHTML, but the editor produces HTML.

So instead of <br /> it produces <br>, etc.....

I have created regular expressions using preg_replace to replace things like this but I'm having trouble with this line:

$text = preg_replace('/(<img .*?)("|\'| )>/i','\1\2 />',$text);

The problem
Now, it replaces <img> tags fine, so that <img src='....' title='....' alt='....'> is converted to <img ........ /> and that is all OK. The problem is that for some reason it also changes <a href='.....'> to <a href='........' /> and I'm not sure why.

Solution
Can someone either suggest a solution to this regular expression, or perhaps a extention or module that is easy to implement that converts HTML to XHTML.

Thank you.

    Excuse me if I'm getting this wrong, but you are changing the HTML output of a web-based editor to XHTML - why don't you just make it output XHTML in the first place, instead of letting it do its job and then correct it?

      cdwhalley.com wrote:

      Excuse me if I'm getting this wrong, but you are changing the HTML output of a web-based editor to XHTML - why don't you just make it output XHTML in the first place, instead of letting it do its job and then correct it?

      The package is a large open-source php based set of scripts. I have had a look through but it is quite complicated. I'll have another look though.

      But you are right. The HTML editor produces HTML code, which I want to convert to XHTML.

        Well, there is no code to replace any <a> tags. I removed all code except for the <img replacement and it still changed the <a ...> to <a ... /> so I believe the problem lies with the code I posted earlier.

        Very strange!

          Try using this, it works for me

          $text ="<a href='http//mysite.com/test.php'>";
          $text = preg_replace('/(<img .*?)("|\'| )>/i','<img\\1\\2 />',$text2);
          echo $text2;
            Houdini wrote:

            Try using this, it works for me

            $text ="<a href='http//mysite.com/test.php'>";
            $text = preg_replace('/(<img .*?)("|\'| )>/i','<img\\1\\2 />',$text2);
            echo $text2;

            It still produces the same problems. E.g. <img changes to <img ... /> but <a> also changes to <a /> which is not needed.

            Here is my EXACT code in full:

            function makeXHTML($text)
            {
                    $text = preg_replace("/<([A-Z]{1,20})(.*?)>/se","'<'.strtolower('\\1').'\\2>'",$text);
                    $text = preg_replace("/<\/([A-Z]{1,20})>/se","'</'.strtolower('\\1').'>'",$text);
            
                for($do=0;$do!=10;$do++)
                $text = preg_replace('/( [a-z,0-9]{1,50})=(?<!")([a-z,0-9]{1,50})( |\/|>)/Si','\\1="\\2"\\3',$text);
            
                [b]$text = preg_replace('/(<img .*?)("|\'| )>/i','<img\\1\\2 />',$text2);[/b]
                $text = preg_replace('/<br>/i','<br />',$text);
                $text = preg_replace('/<hr>/i','<hr />',$text);
            
                $text = preg_replace('/<font.*?size=(\'|"| )2(\'|"| ).*?>/i','<span class="font12px">',$text);
                $text = preg_replace('/<font.*?size=(\'|"| )3(\'|"| ).*?>/i','<span class="font14px">',$text);
            
                $text = preg_replace('/<font.*?>/i','<span class="font10px">',$text);
                $text = preg_replace('/<\/font>/i','</span>',$text);
            
                $text = stripslashes($text);
            
                return $text;
            }
            

            The line in bold is the line you gave me. I've tried commenting ALL lines out and leaving just that one and it still changes <a> to <a /> so it is definetly that, that is causing the problem.

            Thanks for your help.

              Write a Reply...