Solved

Substr Question

Posted on 2007-04-05
12
330 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 look for a specific file type in a local or remote server directory using PHP.
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…

688 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