Jump to content

how to insert the special character <--break-->


Recommended Posts

This is the code ,

This code perform like add the <--break--> in to content with certain character has reached,

this code satisfy the below condition,

 

that is it put the <--break--> after the 150 character has reached,

 

if 150 character before any html has opened then it cose the html close after 200 charater then i ass the <--break--> after html has closed,

 

So here i want to add one more condtion that is , dont broke the sentense,

that is also check if the  comma or fullstop or semicolon, colon near, then add the <--break-->

after the full stop,

 

here is code

 

<?php
$text_ori = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.<p>Lorem Ipsum has been the industry\'s standard dummy text</p>ever since the 1500s, when an unknown printer took a galley of type and scrambled<table><tr><td>it to make a type specimen book.</td></tr></table>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.<b> It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,</b>and more recently with desktop publishing<p>software like Aldus PageMaker</p>including versions of Lorem Ipsum.';
$text = preg_split("/(\<.+\>|&.+;)/sU",$text_ori,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
//split the text based on html tag, it will produce array.
$counter = 50; //number of character, must greater than 0
$counter_temp = $counter;
$random = "<--break-->";//separator => in your case : <--break--> => in html entities <--break>
$add_random = 0; //variable to store how many add randomness
$open_tag = 0; //variable to store how many opentag
//html tag is not counted, html entities is counted as 1 character, comment is not counted

for($i = 0; $i < count($text); $i++)
{//looping in array
    if(!preg_match("/^\</sU",$text[$i]))
    {//array value is not html tag, only non html tag are counted, start count
        if(preg_match("/^&.+;$/",$text[$i]))
        {// array value is html entity
            //consider html entity as 1 character, change later
            $counter_temp--;
            if($counter_temp==0)
            {
                if(!$open_tag)
                {
                    $text[$i] .= $random;
                }
                else
                {
                    $add_random++;
                }
                $counter_temp = $counter;
            }
        }
        else
        {//array value is not contain html tag and entity.
            $length = strlen($text[$i]);
            $counter_temp = $counter_temp - $length;
            if($counter_temp<=0)
            {
                $temporary = $length + $counter_temp;
                $how_much = floor(($length - $temporary) / $counter);
                if($open_tag)
                {//add
                    $add_random+=$how_much;
                    $add_random++;
                }
                else
                {//write
                    for($j = $how_much; $j >=0; $j--)
                    {
                        $test = $temporary + ($j * $counter);
                        $text[$i] = preg_replace("/^.{".$test."}/s","$0".$random,$text[$i]);
                    }
                }
                do
                {
                    $counter_temp +=$counter;
                } while ($counter_temp<=0);
            }
        }
    }
    elseif(preg_match("/^\<.*\/\>$/U",$text[$i]))
    {//self closing tag.
        //do nothing
    }
    elseif(preg_match("/^\<\/\w+\>/",$text[$i]))
    {//closing tag
        //turn off toggle
        $open_tag--;
        if($add_random&&!$open_tag)
        {    
            do
            {
                $add_random--;
                $text[$i] .= $random;
            } while ($add_random!=0);
        }
    }
    else
    {//opening tag
        $open_tag++;
    }
}
if($add_random)
{
    echo "Cannot find closing tag. not valid xhtml or not complete ones.";
}
else
{
    $text = implode($text); //join array
    $text = preg_replace("/(".preg_quote($random).")+/","$1",$text);
    echo htmlentities($text); //output it
}
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.