bharanikumarphp Posted July 7, 2008 Share Posted July 7, 2008 Hi dear How to insert the special character <--break--> after some character count is reach, when we add the content, there some restriction, the content may contain the html tags,and also self closed tags, one more thing the <--insert --> tag must insert after the close tag only when the count is comeding between the open and close tag, this is the sample content, what am going to insert example <p>Lorem Ipsum is <b>simply dummy text of the printing</b> and typesetting industry.</p><--break--> <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</p><-=break--> <p> when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><--break--><table><tr><td> It has survived not only five centuries,</td></tr></table><--break--> but also the leap into electronic typesetting, <p>remaining essentially unchanged. </p><b>It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,</b><p> and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> Why do we use it?<--break--> <p> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p> <--break-->The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here',<b> making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years,</b><--break--> sometimes by accident, sometimes on purpose (injected humour and the like)<--break-->. somethings need to follow in that: 1. assume if the cursor reached the 125 character then put the <--break--> 2. assume if cursor travel in the 100 character on that time between the <p> and </p> 50 character then , put the <--break--> after </p> ... 3.also check the sentense complete with comma or semicolon,or colon or full stop,then only put the <--break-->, Thanks in advance Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 7, 2008 Share Posted July 7, 2008 substr_replace($string, "<--break-->", 125, 0); This will put the string "<--break-->" into $string at the 125th character (including white spaces and line breaks). http://www.php.net/manual/en/function.substr-replace.php Quote Link to comment Share on other sites More sharing options...
bharanikumarphp Posted July 7, 2008 Author Share Posted July 7, 2008 see dear that is replace function, but expecting the code which put the special symbol <--break--> after the 125 character,so assume if totally 500 character, then after every 125 character next , have to put the insert <--break--> so for that we have to write the function, i tried this below code but still am get expected output <?php $text_ori = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.<p>Lorem Ipsum has been the industrys standard dummy text</p>ever since the 1500s, when an unknown printer took a galley <P>of type and </P>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>FREDFASDF<P> It was popularised in the 1960s</p>s with the release of Letraset sheets containing Lorem Ipsum passages,fadsfajdsfl jfsa pjfpaj<p> sfpjaspfjpa <b>sjpkjfp</b> pfkapf</p> pajfp ajpfjajfajsf jafp jspfjs</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 = 5; //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 } ?> but out put is Lorem<--break--> Ipsu<--break-->m is <--break-->simpl<--break-->y dum<--break-->my te<--break-->xt of<--break--> the <--break-->print<--break-->ing a<--break-->nd ty<--break-->peset<--break-->ting <--break-->indus<--break-->try.<p>Lorem Ipsum has been the industry's standard dummy text</p><--break-->e<--break-->ver s<--break-->ince <--break-->the 1<--break-->500s,<--break--> when<--break--> an u<--break-->nknow<--break-->n pri<--break-->nter <--break-->took <--break-->a gal<--break-->ley <P>of type and </P><--break-->scra<--break-->mbled<--break--><table><tr><td>it to make a type specimen book.</td></tr></table><--break-->It <--break-->has s<--break-->urviv<--break-->ed no<--break-->t onl<--break-->y fiv<--break-->e cen<--break-->turie<--break-->s, bu<--break-->t als<--break-->o the<--break--> leap<--break--> into<--break--> elec<--break-->troni<--break-->c typ<--break-->esett<--break-->ing, <--break-->remai<--break-->ning <--break-->essen<--break-->tiall<--break-->y unc<--break-->hange<--break-->d.<b>FREDFASDF<P> It was popularised in the 1960s</p>s with the release of Letraset sheets containing Lorem Ipsum passages,fadsfajdsfl jfsa pjfpaj<p> sfpjaspfjpa <b>sjpkjfp</b> pfkapf</p> pajfp ajpfjajfajsf jafp jspfjs</b><--break-->a<--break-->nd mo<--break-->re re<--break-->centl<--break-->y wit<--break-->h des<--break-->ktop <--break-->publi<--break-->shing<--break--><p>software like Aldus PageMaker</p><--break-->i<--break-->nclud<--break-->ing v<--break-->ersio<--break-->ns of<--break--> Lore<--break-->m Ips<--break-->um. u see i given the 5 character count but in output some breaks inserted with in 4 character count and 1 character count Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 A combination of str_split and implode should do it. Quote Link to comment Share on other sites More sharing options...
bharanikumarphp Posted July 7, 2008 Author Share Posted July 7, 2008 Walk through my code, Find error spot or find wrong in the code, give solution for that... or u have code this siution, Then send that code.. Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 I'm not coding for you. Look up the functions in the PHP manual. If that doesn't help, go back to the basics. Show me an attempt to use the advice I've given and I'll help further. Quote Link to comment Share on other sites More sharing options...
bharanikumarphp Posted July 7, 2008 Author Share Posted July 7, 2008 sorry dear already i crossed the basic, my problem is, my code not giving the exact solution, but i know my logic is corrrect, but some where i made the mistake, so that i coming to forum,,, here is the code, <?php $text_ori = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.<p>Lorem Ipsum has been the industrys standard dummy text</p>ever since the 1500s, when an unknown printer took a galley <P>of type and </P>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>FREDFASDF<P> It was popularised in the 1960s</p>s with the release of Letraset sheets containing Lorem Ipsum passages,fadsfajdsfl jfsa pjfpaj<p> sfpjaspfjpa <b>sjpkjfp</b> pfkapf</p> pajfp ajpfjajfajsf jafp jspfjs</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 = 5; //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 } ?> for that output Lorem<--break--> Ipsu<--break-->m is <--break-->simpl<--break-->y dum<--break-->my te<--break-->xt of<--break--> the <--break-->print<--break-->ing a<--break-->nd ty<--break-->peset<--break-->ting <--break-->indus<--break-->try.<p>Lorem Ipsum has been the industry's standard dummy text</p><--break-->e<--break-->ver s<--break-->ince <--break-->the 1<--break-->500s,<--break--> when<--break--> an u<--break-->nknow<--break-->n pri<--break-->nter <--break-->took <--break-->a gal<--break-->ley <P>of type and </P><--break-->scra<--break-->mbled<--break--><table><tr><td>it to make a type specimen book.</td></tr></table><--break-->It <--break-->has s<--break-->urviv<--break-->ed no<--break-->t onl<--break-->y fiv<--break-->e cen<--break-->turie<--break-->s, bu<--break-->t als<--break-->o the<--break--> leap<--break--> into<--break--> elec<--break-->troni<--break-->c typ<--break-->esett<--break-->ing, <--break-->remai<--break-->ning <--break-->essen<--break-->tiall<--break-->y unc<--break-->hange<--break-->d.<b>FREDFASDF<P> It was popularised in the 1960s</p>s with the release of Letraset sheets containing Lorem Ipsum passages,fadsfajdsfl jfsa pjfpaj<p> sfpjaspfjpa <b>sjpkjfp</b> pfkapf</p> pajfp ajpfjajfajsf jafp jspfjs</b><--break-->a<--break-->nd mo<--break-->re re<--break-->centl<--break-->y wit<--break-->h des<--break-->ktop <--break-->publi<--break-->shing<--break--><p>software like Aldus PageMaker</p><--break-->i<--break-->nclud<--break-->ing v<--break-->ersio<--break-->ns of<--break--> Lore<--break-->m Ips<--break-->um. thing is some time it read only 2 character, thats why i come to forums,,,, not an basic learning Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 7, 2008 Share Posted July 7, 2008 Use preg_replace, then: preg_replace("[([\w|\W]{125})]", "$1<--break-->", $string); That will put "<--break-->" after every 125 characters and you don't even have to check for end of string! Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 His function is quite complex, he wants to ignore html tags in the character count. I'll work up a function. Quote Link to comment Share on other sites More sharing options...
bharanikumarphp Posted July 7, 2008 Author Share Posted July 7, 2008 dear i have to paste ur code into my function or how... u walk through my code dear Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 7, 2008 Share Posted July 7, 2008 You just run the preg_replace function on your string that you want to put the "<--break-->" text into. $text_ori = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.<p>Lorem Ipsum has been the industrys standard dummy text</p>ever since the 1500s, when an unknown printer took a galley <P>of type and </P>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>FREDFASDF<P> It was popularised in the 1960s</p>s with the release of Letraset sheets containing Lorem Ipsum passages,fadsfajdsfl jfsa pjfpaj<p> sfpjaspfjpa <b>sjpkjfp</b> pfkapf</p> pajfp ajpfjajfajsf jafp jspfjs</b>and more recently with desktop publishing<p>software like Aldus PageMaker</p>including versions of Lorem Ipsum.'; $text_ori = preg_replace("[([\w|\W]{125})]", "$1<--break-->", $text_ori); That will change $text_ori to have the break strings in it. Quote Link to comment Share on other sites More sharing options...
bharanikumarphp Posted July 7, 2008 Author Share Posted July 7, 2008 see the code broke the html; tags, then output is Lorem Ipsum<<--break-->/p>[color=red] is simply d<--break-->ummy text of<--break--> the printing a<--break-->nd typesetting[/color] <--break-->industry. L<--break-->orem Ipsum has <--break-->been the indust<--break-->rys standard du<--break-->mmy text ever s<--break-->ince the 1500s,<--break--> when an unknow<--break-->n printer took <--break-->a galley of typ<--break-->e and scrambled<--break--> it to make a t<--break-->ype specimen bo<--break-->ok. It has surv<--break-->ived not only f<--break-->ive centuries, <--break-->but also the le<--break-->ap into electro<--break-->nic typesetting<--break-->, remaining ess<--break-->entially unchan<--break-->ged. It was pop<--break-->ularised in the<--break--> 1960s with the<--break--> release of Let<--break-->raset sheets co<--break-->ntaining Lorem <--break-->Ipsum passages,<--break--> and more recen<--break-->tly with deskto<--break-->p publishing so<--break-->ftware like Ald<--break-->us PageMaker in<--break-->cluding version<--break-->s of Lorem Ipsu<--break-->m char count is 15 u see here html tag are broked... that is between html open and close tag between..the break is inserted... Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 This should do it... this script also assumes all HTML tags are closed, or it just ignores the rest of the string <?php $text_ori = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. <p>Lorem Ipsum has been the industrys standard dummy text</p>ever since the 1500s, when an unknown printer took a galley <P>of type and </P>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>FREDFASDF<P> It was popularised in the 1960s</p>s with the release of Letraset sheets containing Lorem Ipsum passages,fadsfajdsfl jfsa pjfpaj<p> sfpjaspfjpa <b>sjpkjfp</b> pfkapf</p> pajfp ajpfjajfajsf jafp jspfjs</b>and more recently with desktop publishing<p>software like Aldus PageMaker</p>including versions of Lorem Ipsum.'; echo break_up( $text_ori, '<--break-->', 15 ); function break_up( $text, $insert, $chunk = 125 ) { $counter = 0; for ( $i = 0; $i < strlen( $text ); $i++ ) { # Ignore non-space whitespace characters if ( preg_match('/[\t\n\r\f\v]/', $text{$i} ) ) continue; # Check if an HTML tag is opening if ( $text{$i} == '<' ) { # Let's ignore the rest of the tag while ( $text{$i} != '>' ) $i++; continue; } # Increment character count $counter++; # Check if chunk size has been reached if ( $counter > $chunk ) { # Insert string $text = substr_replace( $text, $insert, $i, 0 ); # Increment $i as necessary $i += strlen( $insert ); # Reset counter $counter = 0; } } return $text; } ?> Quote Link to comment Share on other sites More sharing options...
bharanikumarphp Posted July 7, 2008 Author Share Posted July 7, 2008 i thing dear u just consentrat only inserting <--break--> but my point is when we have to insert the <--brak--> after the html tae is closed... that is example Lorem Ipsum <p>is simply dummy text of the printing and typesetting industry.</p> <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</p> <b>when an unknown printer took a galley of type</b><p> and scrambled it to make a type specimen book.</p><p> It has survived not only five centuries,</p> but also the leap into electronic typesetting, remaining essentially unchanged. <li>It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,</li> and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum assum character count is 50 assume if the count is meet inside the <p>is simply dummy text of the printingand typesetting industry.</p> then we have to insert the <--break--> after </p>...not an before of the html close tag...and alser o the sentence must complete then put the <--break--> dont worry the real content counter is 5000, Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 There's too many variables here... can it be ANY closing html statement? What about tables? Do you want the break after a TD or did you want to force it to the </ TABLE > level? Quote Link to comment Share on other sites More sharing options...
bharanikumarphp Posted July 7, 2008 Author Share Posted July 7, 2008 yes dear after table close only want to put the <--break-->, not an table tag only...for all html pair tags, and also the sentence must complete, that is add the <--break--> after the comma or fullstop, other wise the sentense is splited,so we have to avoid this situation also... so my final thing is .. wants to insert <--break-->... that break never broke the html pair tag, and also never seprate the sentence completion Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 So you want to count, but not break inside tags, or break sentences? Quote Link to comment Share on other sites More sharing options...
bharanikumarphp Posted July 7, 2008 Author Share Posted July 7, 2008 exactly dear output must be like this manner <p>Lorem Ipsum is <b>simply dummy text of the printing</b> and typesetting industry.</p><--break--> <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</p><-=break--> <p> when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><--break--><table><tr><td> It has survived not only five centuries,</td></tr></table> but also the leap into electronic typesetting, <p>remaining essentially unchanged. </p><b>It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,</b><p> and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> Why do we use it? <p> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p> The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here',<b> making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years,</b> sometimes by accident, sometimes on purpose (injected humour and the like). Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 What do you consider a new sentence? Just after a period? ( . ) Quote Link to comment Share on other sites More sharing options...
bharanikumarphp Posted July 7, 2008 Author Share Posted July 7, 2008 see dear when after reach the . position then only put the <break> never or any situation we dont broke the sentence.. Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 7, 2008 Share Posted July 7, 2008 You should be searching your text for those areas that allow breaking, then. Assuming it is all text, you shouldn't ever need to but the break string anywhere but after a period. If that is the case, you do it like this: preg_match_all("\.", $string, $matches, PREG_OFFSET_CAPTURE); Then check for the interval $nextlocation = 125; foreach ($matches[0] as $match) { if ($match[1] > $nextlocation) { substr_replace($string, "<--break-->", $match[1], 0); $nextlocation += 125; } } Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 Well, this is very challenging. There are so many things to check for, considering a tag can be opened and closed mid-sentence. This is something you may want to take to the freelance forum to tackle. There's could be upwards of a couple hours of head scratching and testing here. Also keep in mind that people using improper sentence structure would be extremely hard to test for. Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 7, 2008 Share Posted July 7, 2008 Well, this is very challenging. There are so many things to check for, considering a tag can be opened and closed mid-sentence. This is something you may want to take to the freelance forum to tackle. There's could be upwards of a couple hours of head scratching and testing here. Also keep in mind that people using improper sentence structure would be extremely hard to test for. If the "<--break-->" can't be in the middle of the sentence, why would it ever be in the middle of a tag? No sentence will ever end in the middle of a tag. Like I posted above, the only thing you would have to look for is the end of a sentence. I see now that it can be after a comma too, but the logic still works the same, my code would just need to match for commas and periods. Quote Link to comment Share on other sites More sharing options...
bharanikumarphp Posted July 7, 2008 Author Share Posted July 7, 2008 tell reply for my exprected output.. Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 7, 2008 Share Posted July 7, 2008 I don't have any idea what that means. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.