Solved

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

Posted on 2014-12-09
3
68 Views
Last Modified: 2014-12-09
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

0
Comment
Question by:brucegust
  • 2
3 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 40489354
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
 

Author Comment

by:brucegust
ID: 40489520
And that, Tom, is why you get paid the large dollars!

Thanks so much!
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40489566
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now