Solved

Substr Question

Posted on 2007-04-05
12
329 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do use '  ' within this code? 4 41
Why is my $_POST not going to results page 10 62
blocking the posted data from outside of my domains 8 45
php non-object 7 21
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

736 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