Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

another substr question

Posted on 2007-03-30
5
Medium Priority
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

721 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