Jump to content

Locating a starting spot and trimming strings


Bobafart

Recommended Posts

I am display a bunch of news feeds ( http://www.gabbr.com ) .  Some of the newsfeeds have img's at the beginning, other news feeds have 300+ characters.. you can imagine the high variability and heterogeneity that I am having when I display the multiple news feed content.

So, I thought I would cut down the size of the news feed articles to only 200 characters.  That way I wouldn't have hundreds of words displaying for one article taking up valuable space on the front page.  The problem is some of the news articles start with the < img src > tags.  So the first 200 characters may, sometimes, muck up the image. 

I want the images to still display, however I only want the first 200 characters of text to be displayed.  I will use substr($article, 0, 200) to only show the first 200 characters.. but how do I position the counter to start counting AFTER the IMG tag IF there is an IMG to be displayed in the feed?

Is there was a way to position the counter after the end of the last '>' sign after the <img src></img> tag.  Once the counter is there I will then count 200 characters and terminate the article output at that point so that the IMG is still displayed and the article doesn't go on forever.

Does anyone know how to do this?
but wont he get the first one not the second ?
because he metioned <img src> and </img>

and what if he does like setting the text like <font> or somin?

i was thinking soming like

[code]
<?php
#start by putting your info into a array so we can search
$array = array($article);
if(in_array("<img",$array)) {
  #cut it down to 100 maybe?
  $pos = strpos($article,"</img>");
  $article2 = substr($article,0,$pos);
  $article = substr($article,$pos,100);
  $article = $article . $article2;
}else{
  $article = substr($article,0,200);
}
?>
[/code]
[quote]but wont he get the first one not the second ?
because he metioned <img src> and </img>[/quote]

Yeah, silly me.

Could use an offset,

[code=php:0]
$start = strpos($articel,'>',1);
echo substr($article,$start+1,200);
[/code]

but as you said, there could be other tags. I gues there probably isn't really a safe option here. Yours is probably safer, depends on the data I suppose.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.