Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6866945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:14:50+00:00 2026-05-27T03:14:50+00:00

Trying to write such function. It must divide text into multiple columns and the

  • 0

Trying to write such function. It must divide text into multiple columns and the output must be valid html, e.g. no unopened(!!!) close tags and no unclosed tags. Here is my code:

function convert2columns($content = '', $columns = 2) {
$result = array();
$content = closetags($content);
$bodytext = array("$content");
$text = implode(",", $bodytext);
$length = strlen($text);
$length = ceil($length / $columns);
$words = explode(" ", $text);    
$c = count($words);
$l = 0;
for ($i = 1; $i <= $columns; $i++) {
    $new_string = "";
    for ($g = $l; $g <= $c; $g++) {
        if (strlen($new_string) <= $length || $i == $columns) {            
if (in_array(substr(@$words[$g], $length - 1, 1), array(' ', '.', '!', '?')))
                $new_string .= @$words[$g] . " ";
            else {
                $split = substr(@$words[$g], 0, $length - 1);
                $lastSpace = strrpos($split, ' ');
                if ($lastSpace !== false) {
                    $split = substr($split, 0, $lastSpace);
                }
                if (in_array(substr($split, -1, 1), array(','))) {
                    $split = substr($split, 0, -1);
                }
                $new_string .= $split . " ";
            }
        } else {
            $l = $g;
            break;
        }
    }
    $result[] = $new_string;
}
return $result;
}

Works, but When trying to divide some text into 2 columns, I get unclosed tags in first column and unopened in second. How to fix this? Need help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:14:51+00:00Added an answer on May 27, 2026 at 3:14 am

    Here’s my solution. I wanted some code that would be aware of paragraphs, blockquotes, tables etc. so the second column always starts after all tags have been closed.

    function ContentToTwoColumns($fullContent){
      //Get character length of content
      $fullContentLength = strlen($fullContent);
      //Set the ratio of column size
      $column1Percent = .55;
      $column2Percent = .45;
    
      //Break the content into two columns using the ratios given.
      $columnsContent = array();
      $columnBreak = round($column1Percent * $fullContentLength);
      $columnsContent[0] = substr($fullContent, 0, $columnBreak);
      $columnsContent[1] = substr($fullContent, $columnBreak);
    
      //Check for unclosed tags in the first column by comparing
      //the number of open tags with closed tags.
      $numTags = countOpenClosedTags($columnsContent[0]);
      $numOpened = $numTags[0];
      $numClosed = $numTags[1];
      $unclosedTags = $numOpened - $numClosed;
    
      //echo '<p>Opened Tags: '.$numTags[0].'</p>';
      //echo '<p>Closed Tags: '.$numTags[1].'</p>';
      //echo '<p>Unclosed Tags: '.$unclosedTags.'</p>';
    
      //If there are unclosed tags recalculate the column break.
      if($unclosedTags > 0){
    
        //Return the identity of all open tags in the first column.
        preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $columnsContent[0], $result);
        $openedTags = $result[1];
    
        //Return the identity of all closed tags in the first column.
        preg_match_all("#</([a-z]+)>#iU", $columnsContent[0], $result);
        $closedTags = $result[1];
    
        //Reverse array of open tags so they can be matched against the closed tags.
        $openedTags = array_reverse($openedTags);
    
        //Loop through open/closed tags to identify first unclosed tag
        //in first column on content.
        for( $i = 0; $i < $numOpened; $i++ ){
          if ( !in_array ( $openedTags[$i], $closedTags ) ){
            $firstOpenTag = $openedTags[$i];
            //echo '<p>Open Tag: &lt;'.$firstOpenTag.'&gt;</p>';
          } else {
            unset ( $closedTags[array_search ( $openedTags[$i], $closedTags)] );
          }
        }
    
        //Get name of first open tag and create a closed version.
        //$firstOpenTag = $openedTags[$tagNum][0];
        $firstOpenTagClosed = '</'.$firstOpenTag.'>';
    
        //Calculate the tag length of the closed version.
        $tagLength = strlen($firstOpenTagClosed);
    
        //Locate the position of the first closed tag in the second column
        //content that matches the first opened tag in the first column
        $positionCloseTag = strpos($columnsContent[1], $firstOpenTagClosed);
    
        //Calculate the position of the new column break using the
        //position of and length the final closing tag.
        $columnBreak = $columnBreak + $positionCloseTag + $tagLength;
    
        //echo '<p>Final Closing Tag: &lt;/'.$firstOpenTag.'&gt;</p>';
        //echo '<p>Column Break Point: '.$columnBreak.'</p>';
    
        // Break the content into two columns using the new break point. 
        $columnsContent[0] = substr($fullContent, 0, $columnBreak);
        $columnsContent[1] = substr($fullContent, $columnBreak);
       }
    
      // Return the two columns as an array
      return $columnsContent;
    }
    
    function countOpenClosedTags($html){
      //Return the identity and position of all open tags in the HTML.
      preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $result, PREG_OFFSET_CAPTURE);
    
      //Check that the returned result array isn't empty.
      if (!isset($result[1])){
        $numOpened = 0;
      } else {
        //If the result array isn't empty get the number of open tags.
        $openedTags = $result[1];
        $numOpened = (!$openedTags) ? 0 : count($openedTags);
      }
    
      //Return the identity and position of all close tags in the HTML.
      preg_match_all("#</([a-z]+)>#iU", $html, $result, PREG_OFFSET_CAPTURE);
    
      //Check that the returned result array isn't empty.
      if (!isset($result[1])){
        $numClosed = 0;
      } else {
        //If the result array isn't empty get the number of close tags.
        $closedTags = $result[1];
        $numClosed = (!$closedTags) ? 0 : count($closedTags);
      }
    
      //Create an array to return the open and close counts.
      $numTags = array($numOpened, $numClosed);
      return $numTags;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function to update the multiple select (the second one
I am trying to write a Left Justify Function such as: private static string
I'm trying to write a simple rule such that: /page gets implicitly routed to
Im trying to write a function for adding category: function addCategory() { $cname =
I'm trying to write a regex function that will identify and replace a single
I'm trying to write a simple PHP function that can take a string like
Hey all, I'm trying to write a sort function but am having trouble figuring
I am trying to read a simple text file into a String. Of course
I'm trying to write a function that'll detect if a digit is found in
(Important: See update below.) I'm trying to write a function, import_something , that will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.