I need to insert some CSS tags at the beginning and end of a body of text every 145 characters...

Here's the challenge:

I'm documenting some things I've been able to accomplish / learn in the context of a little "code box." You can see it in the attached screenshot. It's purely aesthetic, but I like it. It looks like this:

 screenshot
In order to make it look like that, I have to document things in a way that is somewhat tedious. It looks like this:

<div id="code_container"><ol>
<li><span>&lt;?php</span></li>
<li><span> require_once('adminpageclass.php');</span></li>
<li><span> require_once('csv_AdminClass.php');</span></li>
<li><span> require_once('csv_AdminView.php');</span></li>
<li><span> $new_page= new AdminPage;</span></li>
<li><span> </span></li>
<li><span> $new_tool = new ToolAdmin;</span></li>
<li><span> </span></li>
<li><span> $tool_list=$new_tool-&gt;tool_list();</span></li>
<li><span> </span></li>
<li><span> $body = '&lt;br&gt;&lt;br&gt;&lt;div style="float:right;"&gt;&lt;select name="select62" size="1" onchange="MM_jumpMenu(\'new\',this,1)"&gt; </span></li>
<li><span> &lt;option value="#" selected&gt;Tools&lt;/option&gt;';</span></li>
<li><span> $tool_rows=tool_select($tool_list);</span></li>
<li><span> $body .=$tool_rows;</span></li>
<li><span> $body.='</span></li>
<li><span> &lt;option value=""&gt;_______________________________&lt;/option&gt;</span></li>
<li><span> &lt;/select&gt;&lt;/div&gt;Every time you upload a CSV file into a reporting tool, there\'s always a chance that some of your naming conventions don\'t match the field names of the database you\'re getting ready to interact with. To avoid that, this tool gives you the opportunity to "connect the dots" between your field names and the field names of the appropriate database. &lt;br&gt;&lt;br&gt;At the end of the process, you\'ll have the opportunity to store your preferences so you won\'t have to do this every time you want to use a particular tool. Start by selecting the tool you want to use from the pulldown to the right.&lt;br&gt;&lt;br&gt;To download a sample CSV file to see this app in action, click &lt;A HREF="http://cascsapws1v.nss.vzwnet.com/sandbox/atlanta.csv" target="_blank"&gt;here&lt;/a&gt;. Also, the list of tools that you see displayed in the pulldown menu to the right can be edited using the admin suite which can be accessed by clicking &lt;a href="adm/admin.php" target="_blank"&gt;here&lt;/a&gt;.&lt;br&gt;&lt;br&gt;</span></li>
<li><span> ';</span></li>
<li><span> </span></li>
<li><span> $new_page-&gt;setBody($body);</span></li>
<li><span> </span></li>
<li><span>Every time you upload a CSV file into a reporting tool, there\'s always a chance that some of your naming conventions don\'t</span></li>
<li><span> echo $new_page-&gt;display();</span></li>
<li><span> ?&gt;</span></li> 
</ol></div>

Open in new window


I've got to change the "<" characters to "&lt;" as well as add the <li><span> to each and every line...it's a drag.

The good news is that I built a little interface that does that for me quickly and easily, but there's one dynamic that I've yet to negotiate and that's what you see beginning line 17 (I'm hoping that the EE interface matches what I've got on my box). It's that portion of the sreenshot you see above where there's a bunch of text rather than code. In that scenario, you don't see each line of text on a numbered line. Rather it all shows up as "line 17" and I want to change that.

To do that I've got to add "<li><span>" and "</span></li>" to the beginning and end of every chunk of text that's no more than 145 characters.

It's kind of like taking a "read more" dynamic where you cut a piece of text after a certain point, back up to ensure that you haven't chopped off a word, but here I want to take that portion of the text, add the appropriate "<li><span>" and "</span></li>" characters to the beginning and end, AND THEN do it again to the original body of text sans the portion I just manipulated.

So, if I'm starting with this:

Every time you upload a CSV file into a reporting tool, there\'s always a chance that some of your naming conventions don\'t match the field names of the database you\'re getting ready to interact with. To avoid that, this tool gives you the opportunity to "connect the dots" between your field names and the field names of the appropriate database.

I want to have this:

<li><span>Every time you upload a CSV file into a reporting tool, there\'s always a chance</span></li>
<li><span>that some of your naming conventions don\'t match the field names of the database you\'re</span></li>
<li><span>getting ready to interact with. To avoid that, this tool gives you the opportunity to "connect the dots"</span></li>
<li><span> between your field names and the field names of the appropriate database.</span></li>

I started working on it by taking a block of code that I've used before to create the "read more" scenario that I mentioned above and tried to throw that into a for loop that loops through the sequence according to the number laps dictated by dividing the total nunber of characters by 145.

I was making progress until I determined that it was time to get on the phone with the ninjas.

Here's what I've done. It doesn't work, what do I need to change?

<?php
$stage_one=$_POST['body_code'];//my body of text

$barrier =round((strlen($stage_one) / 145)+1);//calculating the number of times I'm going to have to loop through my text given the way I want to break it up into 145 character chunks
$position=145;
$post="";
$big_array=array();
$a="";
$message_="";
for($a=1; $a<$barrier; $a++)
{	
$message_.$a=="$stage_one";
$post_.$a = substr($message_.$a,$position,1); //the next 8 lines of code grab stage one and reduce it down to a legible collection of words that don't exceed 145 characters  										
	if($post_.$a !=" ") {					
	$length = strlen( $message_.$a );					
		while($post_.$a !=" " && $position < $length){					
		$i =1;					
		$position = $position+$i;					
		$message_.$a = $stage_one;					
		$post_.$a = substr($message_.$a,$position,1); 							
		}					
	}					
$post_.$a = substr($message_.$a,0,$position); 	

$stage_two_.$a=$post_.$a;//here's where I'm adding the <li><span> tags to the beginning of the 145 character chunk and </span></li> to the end
$stage_three_.$a=str_replace("&lt;", "&amp;lt;", "$stage_two_.$a");
$stage_four_.$a=str_replace("&lt;", "&amp;lt;", "$stage_three_.$a");
$stage_five_.$a="&lt;li&gt;&lt;span&gt;";
$stage_six_.$a="&lt;/span&gt;&lt;/li&gt;";
$stage_seven_.$a=$stage_five_.$a.''.$stage_four_.$a.''.$stage_six_.$a; //the finished string

$big_array=$stage_seven_.$a;//stuffing each manipulated string into an array

$round_.$a=(strlen($stage_four_.$a))-1; //calculating the number of logical characters that make up my manipulated string
$stage_one = substr($stage_one, $round_$a); //subtracting the number of characters that I just calculated in the proceeding line from my body of text and then starting the process all over again		

}
?>

Open in new window

brucegustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
How about splitting into words instead of characters?
$complete = 'Every time you upload a CSV file into a reporting tool, there\'s always a chance that some of your naming conventions don\'t match the field names of the database you\'re getting ready to interact with. To avoid that, this tool gives you the opportunity to "connect the dots" between your field names and the field names of the appropriate database.';
$words = preg_split("/[\s]+/",$complete);
$output .= "<li><span>";
$spaces = 0;
foreach ($words as $word) {
	$count += strlen($word);
	if ($count < (123 - $spaces)) { //145 - 22 = 123. 22 is the number of characters in "<li><span></span></li>"
		$output .= " " . $word;
		$spaces++;
	} else {
		$output .= "</span></li><li><span>" . $word;
		$count = 0;
		$spaces = 0;
	}
}
echo $output .= "</span></li>";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brucegustPHP DeveloperAuthor Commented:
And that, Tom, is why you get paid the large dollars!

Thanks so much!
0
Tom BeckCommented:
Cool. I'm a PHP programmer now!
I changed my code to include spaces in the count and exclude the HTML tags after my initial post. Wasn't sure if you realized that I sneaked that in.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.