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?
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.