Link to home
Start Free TrialLog in
Avatar of Larry Vollmer
Larry Vollmer

asked on

another substr question

sometimes teh first portion of the text brought in by post_title is an <img src="....>

here is what is returned in that instance

<div class="blogs_story_2_img">
<a href="http://suburbarazzi..com/"><img src="http://www..com/includes/suburbarazzi/button.gif" alt="Secondary" width="100" height="75" border="0" /></a></div>
<div class="blogs_story2"><a href="http://suburbarazzi..com/"><img...</a></div>


is there a way to say if post_content = <img... then echo post_title instead?


function max_len($string, $limit = 85)
{
      $words = preg_split('# #', $string, -1, PREG_SPLIT_NO_EMPTY);
      $newstring = '';

      for ($i = 0; strlen("$newstring{$words[$i]} ") <= $limit; $i++)
      {
            $newstring .= "{$words[$i]} ";
      }
      return rtrim($newstring);
}

if ($r = mysql_fetch_array($result))
{
      $domain1 = $r['domain'];
      $post_title1 = $r['post_title'];
      $post_content1 = max_len($r['post_content'], 65);
      //$blog_id1 = $r['blog_id'];
      $button1 = $r['button'];
      $summary1 = $r['summary'];
}

if ($r = mysql_fetch_array($result))
{
      $domain2 = $r['domain'];
      $post_title2 = $r['post_title'];
      $post_content2 = addslashes(max_len($r['post_content'], 65));
      //$blog_id2 = $r['blog_id'];
      $button2 = $r['button'];
      $summary2 = $r['summary'];
}

if ($r = mysql_fetch_array($result))
{
      $domain3 = $r['domain'];
      $post_title3 = $r['post_title'];
      $post_content3 = addslashes(max_len($r['post_content'], 65));
      //$blog_id3 = $r['blog_id'];
      $button3 = $r['button'];
      $summary3 = $r['summary'];
}

if ($r = mysql_fetch_array($result))
{
      $domain4 = $r['domain'];
      $post_title4 = $r['post_title'];
      $post_content4 = addslashes(max_len($r['post_content'], 65));
      //$blog_id4 = $r['blog_id'];
      $button4 = $r['button'];
      $summary4 = $r['summary'];
}
Avatar of wakemup
wakemup

Yes, use:

if (substr($post_content, 0, 4) == '<img'))

Also, to get the first 65 characters of a string just go:

$string = substr(trim($string), 0, 85);

You should not be getting any html page content in the $_POST array, though. Double check your quotes in the source page. Could you post the code from the page that you are posting from?

Avatar of b0lsc0tt
You can test to see if the start of that variable is "<img" but I'm not really clear on what variable or what you mean by "echo" since none of the lines in the code use it.  The basic idea to test would be ...

if (substr($r['post_content'], 0, 4) == '<img') {

}

$r['post_content'] was a guess though so I may be wrong.  Let me know if there is chance the tag may not just use all lowercase img.  Please clarify what I mentioned above if you want me to help with the rest.  Let me know if you have a question.

bol
Avatar of Larry Vollmer

ASKER

<div class="blogs_story_2_img">
<a href="http://<? echo $domain4; ?>/">
<img src="<? echo $button4; ?>" alt="Secondary" width="100" height="75" border="0" /></a>
</div>
<div class="blogs_story2"><a href="http://<? echo $domain4; ?>/"><? echo stripslashes($post_content4); ?>...</a></div>


I suppose there is a chance if the code could be in caps.
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America 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
I'm glad that helped.  Thanks for another fun question, the grade and the points.

bol