1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Shortan String, while exluding html tags.

Discussion in 'PHP' started by love_bug, Nov 1, 2007.

  1. #1
    Hi,
    function below does shorten strings.. it's a good code, but sometimes it chops off html image tags too ,
    I want to modify it so that it can shortain text while ignoring <IMG> tags. please help thank you..

      function ShortenString($text) {
    
            // Change to the number of characters you want to display
            $chars = 400;
    
            $text = $text." ";
            $text = substr($text,0,$chars);
            $text = substr($text,0,strrpos($text,' '));
    		$text = $text."..."; //for htaccess links
            //$text = $text."...</a>]</font>"; //Original LInk
    
            return $text  ."<br>";
    
        }
    PHP:

    SEMrush
     
    love_bug, Nov 1, 2007 IP
    SEMrush
  2. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
    #2
    jakomo, Nov 1, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    This should do what you want.

    
    function ShortenString($text, $chars = 400)
    {
    	if (strpos(substr($text, 0, $chars), '<') !== false)
    	{
    		// Opening HTML tag found within the first 400 chars
    		return substr($text, 0, strpos($text, '>') + 1) . '...';
    	}
    	
    	return substr($text, 0, $chars) . '...';
    }
    
    PHP:
     
    nico_swd, Nov 1, 2007 IP
  4. love_bug

    love_bug Member

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    thank you so much... it helped me a lot.
     
    love_bug, Apr 10, 2008 IP