?
Solved

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

Posted on 2009-02-21
22
Medium Priority
?
696 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:ariestav
  • 14
  • 8
22 Comments
 
LVL 15

Accepted Solution

by:
babuno5 earned 2000 total points
ID: 23703405
i hope this can help
<?php
//capturing the content to send to the truncate function
 
$tmp_str  = "a s d f a d s f a s d f as   sp ace d f ad fs d first scriipt tage<script>alert('hello world')</script>end of script taga<a>startof A tagas df    in si de a ta g as df ad f</a>af te r a ta g asd fa sd fa ss pa ce    sp ac ed fa sd f asdfasdfasdfasdfasdfasdfadfdsafasd fadf asdfasdf adfadsfas fadsfasdf asdf asdf asdf sdf sadf afasdf af fasd fasdfasd fs";
 
 
$blurb = strip_html_tags($tmp_str);
 
 
//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.' . . .';
        
}
 
 
/**
 * Remove HTML tags, including invisible text such as style and
 * script code, and embedded objects.  Add line breaks around
 * block-level tags to prevent word joining after tag removal.
 */
function strip_html_tags( $text )
{
    $text = preg_replace(
        array(
          // Remove invisible content
            '@<head[^>]*?>.*?</head>@siu',
            '@<style[^>]*?>.*?</style>@siu',
            '@<script[^>]*?.*?</script>@siu',
            '@<object[^>]*?.*?</object>@siu',
            '@<embed[^>]*?.*?</embed>@siu',
            '@<applet[^>]*?.*?</applet>@siu',
            '@<noframes[^>]*?.*?</noframes>@siu',
            '@<noscript[^>]*?.*?</noscript>@siu',
            '@<noembed[^>]*?.*?</noembed>@siu',
          // Add line breaks before and after blocks
            '@</?((address)|(blockquote)|(center)|(del))@iu',
            '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
            '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
            '@</?((table)|(th)|(td)|(caption))@iu',
            '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
            '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
            '@</?((frameset)|(frame)|(iframe))@iu',
        ),
        array(
            ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
            "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
            "\n\$0", "\n\$0",
        ),
        $text );
    return strip_tags( $text );
}  
 
?>

Open in new window

0
 
LVL 4

Author Comment

by:ariestav
ID: 23706389
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. . .
0
 
LVL 4

Author Comment

by:ariestav
ID: 23706430
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:babuno5
ID: 23707707
this example which i posted is with sample content and it works for me

Can you post the actual content of the $blurb variable.
0
 
LVL 4

Author Comment

by:ariestav
ID: 23711711
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.

0
 
LVL 15

Expert Comment

by:babuno5
ID: 23711764
can you attach the content in a sample txt file
0
 
LVL 4

Author Comment

by:ariestav
ID: 23711953
Sorry for that.  Here is the attached .txt file.

sample.txt
0
 
LVL 15

Expert Comment

by:babuno5
ID: 23713350
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
0
 
LVL 4

Author Comment

by:ariestav
ID: 23713800
where would I place the error_reporting(1); code?
0
 
LVL 15

Expert Comment

by:babuno5
ID: 23717613
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);
 
?>
0
 
LVL 4

Author Comment

by:ariestav
ID: 23717678
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

0
 
LVL 15

Expert Comment

by:babuno5
ID: 23717756
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
0
 
LVL 4

Author Comment

by:ariestav
ID: 23734292
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. . .
0
 
LVL 4

Author Comment

by:ariestav
ID: 23745110
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
0
 
LVL 4

Author Comment

by:ariestav
ID: 23745333
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. ..
0
 
LVL 4

Author Comment

by:ariestav
ID: 23745652
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?
0
 
LVL 15

Expert Comment

by:babuno5
ID: 23746645
http://in.php.net/settype

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


0
 
LVL 15

Expert Comment

by:babuno5
ID: 23746682
you code looks fine


just try the code with error reporting on

attached is the code for same
test.txt
0
 
LVL 4

Author Comment

by:ariestav
ID: 23798217
Still, no error displayed.  I put on error reporting on the linked files as well.  I'm still at a loss. . .
0
 
LVL 4

Author Closing Comment

by:ariestav
ID: 31549707
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!
0
 
LVL 4

Author Comment

by:ariestav
ID: 23817539
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

0
 
LVL 4

Author Comment

by:ariestav
ID: 23817798
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

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

829 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