Link to home
Start Free TrialLog in
Avatar of ariestav
ariestav

asked on

Why is this truncate function not working after using PHP strip_tags() function?

Hi There,

I am trying to truncate some text that is being returned by the strip_tags() function built into PHP.  If I give the truncate function any text it usually will work properly, but the second I send it data returned from the strip_tags() function, the truncate function does not return anything except for the elipses ". . . " that is concatenated at the end of the function.

The other problem I am having, is that the strip_tags() function does not eliminate the script text in between two <script> </script> tags.  Why would I want the javascript?

The objective is to essentially return a very short version of a blog entry in a CMS system, and some of the blog entries contain javascript code for embedding video.  The problem is that I only want the text to be returned so I can format it in a "summary" like way.  How can I ensure that the actual script text is taken out as well as truncating the text.  The following is the code for truncating a string of text, as well as how I am calling it.

Your help is greatly appreciated.
<?php
//capturing the content to send to the truncate function
$blurb = strip_tags($row_blogRecent['blog_content']);
 
//calling the truncate function
echo limitByWords($blurb, 50);
 
?>
 
 
//the truncate function
function limitByWords($copy, $limit){
	
	 $numwords = $limit;
	 preg_match("/([\S]+\s*){0,$numwords}/", $copy, $regs);
	 $shortCopy = trim($regs[0]);
	 
	 return $shortCopy.' . . .';
	
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of babuno5
babuno5
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ariestav
ariestav

ASKER

Hi Babuno,

I tried your code in my CMS, but it didn't work.  All I am getting is the ". . ." returned from the limiByWords functions.  Any more thoughts?

Thanks so much for your help. . .
I should clarify that the variable $blurb is actually getting the correct text.  But when I call my limitByWords function by passing it $blurb, then I'm not getting it truncated.
this example which i posted is with sample content and it works for me

Can you post the actual content of the $blurb variable.
The blurb variable contains the entire blog entry content.  I am running your code within a loop so that I can display summaries (or blurbs) of different blog entries.   Does that help?

Here is instance of the $blurb variable before it is truncated:

This blog entry is intended to be a resource for designers working on user experience (UX) or interaction (IxD) storyboards for website applications.  The storyboard template files within the downloadable archive  are intended to be printed out and sketched on.  The browser frames provide a context for the application interface, and they help the project stakeholders visualize the end-user experience.  The image on the right shows the template within InDesign.  Read on to learn how to seamlessly integrate your sketches into the template.
Download the website application storyboard templates | ZIP Archive | 1MB
Files contained in the archive are compatible with Adobe InDesignCS3 or higher and Adobe Illustrator CS or higher. The templates are in Tabloid format (17&quot; x 11&quot;).
From my experience,  when storyboard panels get too large, it seems  the quality of the sketch is reduced or too much time is spent drawing out minute details just to fill up the frame.  Therefore, I made the panels in the template not too large, but not too small.  Their size gives ample space for quick gestures when sketching out the composition of graphical user interfaces, and also provides room for adding detail when needed. With this storyboard template, a UX designer can work with ten  panels which is typically enough to communicate a few interactions and interface state changes.
Before initiating any sketchwork, it's  useful to have developed a scenario-of-use narrative that outlines a workflow for accomplishing specific tasks. The narrative is usually based on a sets of requirements gathered early in the design process.
Section 1 | Storyboarding in Adobe InDesign
Follow the steps below to create a deliverable storyboard by using Adobe InDesign.        The general idea is to sketch on  printed panel frames, scan the sketches, crop out individual sketches from the original scan, and link those individual sketched graphics to  objects within the InDesign document.
1.1
Print multiple pages of  blank panels of the individual panels by printing out the PDF file included in the archive. You should get pages as seen in the image on the right.
1.2
With a pencil, sketch out the application's interface in each panel according to a user's scenario-of-use.  Ink over specific pencil lines if necessary. Some scanners have a difficult time picking up pencil.
1.3
Scan the pages of your sketchwork into Adobe Photoshop. Ensure the scanning resolution for the storyboards is at 300dpi.

Save these original scans to your drive.
1.5           a

Color correct the original scans in order to
accentuate the sketched lines. First, Invoke the
                   Image > Adjustments > Desaturate
command                    from the application menu.

b

Adjust contrast with Image > Adjustments > Levels.  
If using pencil for sketchwork, use the mid-range
slider for best results. Save the changes to the file.

c

Now, duplicate the color corrected canvas in order to alter a new image. Use Image > Duplicate.

d

Crop the duplicate image  to the edges of the first panel sketch. The browser frame should be cropped out as shown in the screenshot. Save the cropped image as a Photoshop file. Repeat steps c and d for each panel until all of the panels have been cropped and saved into individual files. Use  a naming convention, for example:
panel01.psd,panel02.psd, panel03.psd, . . .

1.6
Open the Adobe InDesign storyboard template file included in the archive. Ensure  the document is set to View > Show Frame Edges. The edges around all  graphical objects will be dotted.  This means that those objects are linked to the A-Master page which is essentially a template layout. Changing the objects on the first page requires detaching its objects from the Master Page. Within the Pages panel,  select the first page icon, and invoke its contextual menu. Click on the                Override All Master Page Items command. After doing this, the edges around all graphic objects will switch                  from dotted to solid lines.
Those objects are now editable.
1.7
With the Direct Selection Tool, select the  red picture frame object in the first panel. Once selected, invoke the File > Place command from the main menu. Find the corresponding Photoshop file that you created earlier in Step 1.5. Your sketch should now appear within the storyboard frame. Repeat this step for all picture frame objects on the page.
1.8
Using the text tool, select the various  text boxes to change the labels and narrative.
1.9
If you need  more pages for your storyboard, simply drag the A-Master
page icon in the Pages palette to the
area directly after the Page 1 icon. Then repeat the steps above.
Once all graphic files are loaded into their corresponding frames, export the document to PDF format for delivery.

Section 2 | Storyboarding with Adobe Illustrator
Adobe Illustrator has the capability to help you produce storyboards as well, but as an application it is geared more towards illustration rather than publishing. If you only have access to Adobe Illustrator, you can follow the steps below to create a deliverable storyboard in Adobe Illustrator. The following assumes that steps 1.1
through 1.5             were performed by the user.
2.1        
Open the Adobe Illustrator storyboard template  included in the archive.
Choose File >Place . . . , then select the first graphic file created from step 1.5 in which the scans were duplicated, cropped, and saved.                The sketch will appear atop the document.
2.2
Choose View > Outline from the main menu to see the objects in their structural form. Outline mode only shows the anchor points and paths of the graphical objects. Moving objects in this view eases the task of selecting, moving, and aligning anchor points.
2.3
Ensure that Smart Guides are enabled by selecting the View menu and verifying that a check mark is next to the Smart Guides command. Using the Direct Selection Tool,               click and hold  the graphic's upper left anchor point when the Smart Guide reads &quot;anchor&quot;. Then drag the mouse until the object's upper left achor point is aligned with its                frame's upper left anchor point. See the images on the right for display information.
Repeat placing and aligning the
remainder of the panels as in
step 2.1 and 2.3. This is the bulk of
the work, so save often.
2.4
If you need another page for your Adobe Illustrator storyboard create a new empty
template and repeat the steps above. Once you have completed placing and aligning
the graphic files, export the document to PDF format for delivery.

can you attach the content in a sample txt file
Sorry for that.  Here is the attached .txt file.

sample.txt
i tried with this text also it is working for me

can you try with and check the result
error_reporting(1);
This will show up any warning or error if any
where would I place the error_reporting(1); code?
this will be the first line of code

<?php
error_reporting(1);

//capturing the content to send to the truncate function
$blurb = strip_tags($row_blogRecent['blog_content']);
 
//calling the truncate function
echo limitByWords($blurb, 50);
 
?>
Unfortunately, I'm not seeing any errors or warnings with the following code in the blog page.  I really don't understand why my limitByWords function is not outputting anything.  I haven't changed it since I originally posted the problem.  How else might I troubleshoot?

<?php do { ?>
        				<div style="display:block; clear:left; padding:5px; border:1px dotted gray; margin-bottom:10px; background: #F7F7F7;">
                        	<a href="blog.php?bID=<? echo $row_blogRecent['ID']; ?>" style="font-size:10px; display:block; padding: 5px; font-weight:bolder;"><? echo $row_blogRecent['headline'] ?></a>
                            <span style="font-size: 10px; padding-left: 5px; padding-right: 5px; line-height:14px; margin:0px; display:block;">
								<? error_reporting(1); $blurb = strip_html_tags($row_blogRecent['blog_content']); echo limitByWords($blurb, 100); ?>
                    		</span>                    
                       	</div>
 
            <? $i++; } while ($row_blogRecent = mysql_fetch_assoc($blogRecent)); ?>

Open in new window

error reporting should be placed at the top of a php page that is where it starts executing the php code

Can you attach the complete php file which is getting executed
Sorry for the delay.  I wish I could, but there is some proprietary code in there that I can't post :(

I've been trying to troubleshoot, but to no avail.  I'm going to try to execute the code in a blank html file, and get back to you. . .
Okay, while this isn't the exact file, it is a smaller version.  Again, the database queries are working fine, and the $blurb variable has the long string.  

I am thinking that I need to forcibly typcast the $blurb variable into a string?  Would that be the problem for the limitByWords function?

Thanks!

test.txt
Also, I should note that I copy and pasted the output of the strip_html_tags function into a temporary variable using quotation marks around it, and the limitByWords function worked properly.  Is the strip_html_tags returning a string type that can be processed by a regular expression?

I also tried using the code

settype($text, "string");

in the strip_html_tags function but that didn't do the trick either. . . .

I am at a loss as to why this problem is happening. ..
When I use this code in the strip_html_tags function:

return strip_tags(settype($text, "string"));

the limitByWords function returns "1".

Why might it do that?
http://in.php.net/settype

This function returns a boolean and hence  limitByWords is returning 1 and hence settype($text, "string") is not required


you code looks fine


just try the code with error reporting on

attached is the code for same
test.txt
Still, no error displayed.  I put on error reporting on the linked files as well.  I'm still at a loss. . .
This is really a great help.  Babuno really provided exactly what I was looking for, and I am impressed with the code.  The code works when outputting the result directly.  My only problem is that I don't know how to integrate it into my blog so that it appears correct.  Good job Babuno!
I placed this code in the limitByWords function, and this is the output



function limitByWords($copy, $limit){
 
	 $numwords = $limit;
	 preg_match("/([\S]+\s*){0,$numwords}/", $copy, $regs);
	 $shortCopy = trim($regs[0]);
	 
	 var_dump($regs);
	 //return $shortCopy.' . . .';
	
}
 
//output
array(1) { [0]=>  string(0) "" }
 
so in the $regs array, nothing is there?  Why is that the case?

Open in new window

I found the problem!

The limitByWords function did not take the $copy variable with any whitespace at the beginning, so I wrapped a trim function around the $copy variable in the preg_match function.

Here is the corrected code for the limitByWords function:



function limitByWords($copy, $limit){
 
	 $numwords = $limit;
	 $matchPattern = "/([\S]+\s*){0,$numwords}/";
	 preg_match($matchPattern, trim($copy), $regs);
	 $shortCopy = trim($regs[0]);
	 
	 return $shortCopy.' . . .';
	
}

Open in new window