Solved

Substr Question

Posted on 2007-04-05
12
326 Views
Last Modified: 2013-12-13
I have some code - it looks in the post_content variable for the first four characters. If the first four characters are <img then it defers to the post_title...

I need it to look in the first 65 c haracters. If there is a '<' character, then defer to post_title.

How can I do this?

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'], 60);
      //$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'], 60));
      //$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'], 60));
      //$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'], 60));
      //$blog_id4 = $r['blog_id'];
      $button4 = $r['button'];
      $summary4 = $r['summary'];
}

mysql_close($link);
?>


<?php
function pickContent($title, $content) {
    if (strtolower(substr($content, 0, 4)) == '<img') {
        $result = $title;
    } else {
        $result = $content;
    }
    return $result;
}
?>
0
Comment
Question by:lvollmer
  • 4
  • 4
  • 4
12 Comments
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 18859318
You could just use substr() to grab the first 65 characters of the string, then use strpos to search for the character.  Example:

$content_to_search = substr($post_content, 0, 65);
if (strpos($content_to_search, "<") != FALSE) {
     // defer to post title, the character exists
} else {
    // the character does not exist in the first 65 characters
}
0
 

Author Comment

by:lvollmer
ID: 18859380
where do I put that?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18859650
I would use an expression (preg_match) instead.

<?php
function pickContent($title, $content) {
    $expression = '/<img/';
    if (preg_match($expression, strtolower(substr($content, 0, 65)))) {
        $result = $title;
    } else {
        $result = $content;
    }
    return $result;
}
?>

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

bol
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 15

Expert Comment

by:Tomeeboy
ID: 18859980
I typically try to avoid using preg functions if it's something as simple as this, as they are usually not the most efficient option.  If you're looking for just one character, I'd be willing to bet that strpos() would be the better choice here.

As b0lsc0tt shows, the code would cod in your pickContent() function.  An alternative to the preg_match code that he suggested would be something like this:

<?php
function pickContent($title, $content) {
    if (strpos(substr($content, 0, 65), "<") != FALSE) {
        $result = $title;
    } else {
        $result = $content;
    }
    return $result;
}
?>

As requested, this would search for the "<" character in the first 65 characters of $content.
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 100 total points
ID: 18860082
I agree with Tomeeboy that strpos() is preferred when searching for just one character.  I understood you wanted "<img" so preg_match will work better.  If you do use strpos() then I would suggest using !== false for the operator instead of !=.  It is safer.

If you do want to test "<img" and use preg_match let me know if you have a question or need more info.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18860107
In case the way the line broke confuses it the operator I suggested as a small improvement if you use strpos is !==.  For example (using Tomeeboy's good example) ...

    if (strpos(substr($content, 0, 65), "<") !== FALSE) {

bol
0
 

Author Comment

by:lvollmer
ID: 18860194
how do I put that all together?
0
 
LVL 15

Accepted Solution

by:
Tomeeboy earned 400 total points
ID: 18860337
If we're just modifying the function you already had in place, there's really nothing to put together. You just need to copy and paste the new pickContent function over the one in your original code (unless you were wanting to do something else that I'm not aware of).  Like this:

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'], 60);
      //$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'], 60));
      //$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'], 60));
      //$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'], 60));
      //$blog_id4 = $r['blog_id'];
      $button4 = $r['button'];
      $summary4 = $r['summary'];
}

mysql_close($link);
?>

<?php
function pickContent($title, $content) {
    if (strpos(substr($content, 0, 65), "<") !== FALSE) {
        $result = $title;
    } else {
        $result = $content;
    }
    return $result;
}
?>
0
 

Author Comment

by:lvollmer
ID: 18860699
       $result = $title;
    } else {
        $result = $content;


shouldn't these be post_title and post_content?
0
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 18860764
No, as those are just the variable names used within the pickContent() function.  If the names of those variables are $post_title and $post_content in your script, then the place to use those names would be when the pickContent function is actually called.  For example:

$myresult = pickContent($post_title, $post_content);

Hope that made sense :)
0
 

Author Comment

by:lvollmer
ID: 18860781
ahh i see. Cool thanks
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18861020
I'm glad I could help.  Thanks for the fun question, grade, and points.

bol
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
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.

778 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