Solved

another substr question

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

630 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