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
Solved

Substr Question

Posted on 2007-04-05
12
327 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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 …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…

808 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