Jump to content

Recommended Posts

Hi,

 

I'm got a function from a user note on php.net, which adds the closing html tags to the end of a html string if that tags has'nt been closed, however I'd like for it to display the line number of the original tag (which has'nt been closed), next to the closed tag.

 

Heres the function:

 

<?php
function close_tags($text) {
    $patt_open    = "%((?<!</)(?<=<)[\s]*[^/!>\s]+(?=>|[\s]+[^>]*[^/]>)(?!/>))%";
    $patt_close    = "%((?<=</)([^>]+)(?=>))%";
    if (preg_match_all($patt_open,$text,$matches))
    {
        $m_open = $matches[1];
        if(!empty($m_open))
        {
            preg_match_all($patt_close,$text,$matches2);
            $m_close = $matches2[1];
            if (count($m_open) > count($m_close))
            {
                $m_open = array_reverse($m_open);
                foreach ($m_close as $tag) $c_tags[$tag]++;
                foreach ($m_open as $k => $tag)    if ($c_tags[$tag]--<=0) 
                if($tag != "?php" && $tag != "br"  && $tag != "img"){
                $text.='</'.$tag.'>';
                }
            }
        }
    }
    return $text;
}

$html = '<body>
hey....<tr>';
highlight_string(close_tags($html));

?>

 

Which would output:

 

<body>
hey....<tr></tr></body>

 

However I'd like it to output (the line number of the opening tag next to the closing tag):

 

<body>
hey....<tr></tr>(line 2)</body>(line 1)

 

All help greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/200292-add-line-nums/
Share on other sites

Off the top of my head I can't think of anything...although I have a questions.  Why do you need to close the tags?  Would it not just be easier to make sure they are closed when you are coding?  Or is this intended as some sort of validator?

 

Its some sort of validator and I'd need the line nums for debugging  :P (I got some code which consists of php code and html - not my code im just helping to debug, but the html does'nt pass w3 validation, looking at it; some of the tags are not closed, but I can't manually flick through the code to see which arn't closed as the html is nested and would confuse me).

 

So that function works perfect, but I'd require the line nums so i can then manually check where the tags are opened and hopefully correct them by placing the closed tag in the correct place.

 

I've looked at file() function, which gives line nums, but not sure how to integrate it.

Link to comment
https://forums.phpfreaks.com/topic/200292-add-line-nums/#findComment-1051165
Share on other sites

I really don't know how good of an answer this is - but I believe it is a workable answer, nonetheless.

 

Create a temp txt file on the server, dump all of your data there, and then use the file function(s) to get the corresponding line number.

 

You get where I'm going with that?

Link to comment
https://forums.phpfreaks.com/topic/200292-add-line-nums/#findComment-1051188
Share on other sites

Heres what I've come up with, but the line numbers are not correct (they are not the correct ones for the opening tags):

 

<?php
  function close_tags($text)
  {
      $patt_open = "%((?<!</)(?<=<)[\s]*[^/!>\s]+(?=>|[\s]+[^>]*[^/]>)(?!/>))%";
      $patt_close = "%((?<=</)([^>]+)(?=>))%";
      if (preg_match_all($patt_open, $text, $matches)) {
          $m_open = $matches[1];
          
          $m_lines = array();
          $i = 0;
          $data = str_replace(array("\r\n", "\r"), "\n", $text);
          $data = explode("\n", $text);
          foreach ($m_open as $m_tag)
              for ($line = 0; $line < count($data); $line++) {
                  if (strpos($data[$line], $m_tag) !== false) {
                      $i++;
                      $m_lines[$i] = $line;
                  }
              }
          
          if (!empty($m_open)) {
              preg_match_all($patt_close, $text, $matches2);
              $m_close = $matches2[1];
              if (count($m_open) > count($m_close)) {
                  $m_lines = array_slice($m_lines, 0, count($m_open));
                  $m_lines = array_reverse($m_lines);
                  $m_open = array_reverse($m_open);
                  
                  foreach ($m_close as $tag)
                      $c_tags[$tag]++;
                  $c = array_combine($m_lines, $m_open);
                  foreach ($c as $line => $ctag) {
                      if ($c_tags[$tag]-- <= 0) {
                          $text .= "\n</{$ctag}>(Line {$line})";
                      }
                  }
              }
          }
      }
      $text = preg_replace('~</(img|\?php|input|br)>\(Line ([0-9]+)\)~', '', $text);
      return $text;
  }
?>

 

Any help?

Link to comment
https://forums.phpfreaks.com/topic/200292-add-line-nums/#findComment-1051291
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.