Solved

another substr question

Posted on 2007-03-30
5
258 Views
Last Modified: 2013-12-13
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'];
}
0
Comment
Question by:lvollmer
  • 3
5 Comments
 
LVL 2

Expert Comment

by:wakemup
ID: 18825321
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?

0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18825345
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
0
 

Author Comment

by:lvollmer
ID: 18825501
<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.
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 18825658
>> I suppose there is a chance if the code could be in caps. <<

Use strtolower() function then before you test for a match to '<img'.  For example ...

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

In the code you posted it would probably be best to make a function to then return either $post_content or $post_title.  I'm still not real clear on where those are from but using the code you just posted the idea would be ...

<?php
// function place above the html below
function pickContent($title, $content) {
    if (strtolower(substr($content, 0, 4)) == '<img') {
        $result = $title;
    } else {
        $result = $content;
    }
    return $result;
}
?>

Then your html would now be something like ...

<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(pickContent($post_title4, $post_content4)); ?>...</a></div>

Let me know how that works or if you have a question.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18826405
I'm glad that helped.  Thanks for another fun question, the grade and the points.

bol
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
<? versus <?php 5 37
Php pie charts 3 26
phpmailer in WHILE loop - weird results 10 27
Problem sending file attachments 8 23
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now