Solved

Substr Question

Posted on 2007-04-05
12
324 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
Comment Utility
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
Comment Utility
where do I put that?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
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
 
LVL 15

Expert Comment

by:Tomeeboy
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:lvollmer
Comment Utility
how do I put that all together?
0
 
LVL 15

Accepted Solution

by:
Tomeeboy earned 400 total points
Comment Utility
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
Comment Utility
       $result = $title;
    } else {
        $result = $content;


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

Expert Comment

by:Tomeeboy
Comment Utility
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
Comment Utility
ahh i see. Cool thanks
0
 
LVL 54

Expert Comment

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

bol
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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

12 Experts available now in Live!

Get 1:1 Help Now