Solved

another substr question

Posted on 2007-03-30
5
257 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

762 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

17 Experts available now in Live!

Get 1:1 Help Now