Announcement

Collapse
No announcement yet.

Page title length and repeating words

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Page title length and repeating words

    Afternoon all, I've been playing around with the AUG php for marketing lists that limits the number of words. I've added a line to stop the words being repeated for use to generate a page title, but have a problem if the word original word count is less than the defined cut off.

    PHP Code:
        $sShort "";
        
    $nCount 0;
        
    $sOriginal "some string";
        foreach(
    explode(" "$sOriginal) as $sWord)
            {
            if (
    $nCount 12)
                {
                
    $sShort .= "...";
                break;
                }
            if (
    strpos($sShort$sWord) !== false) continue;
            
    $sShort .= $sWord " ";
            }
        echo 
    $sShort
    If $sOrigininal has fewer than the 12 words defined in $nCount, then I get the error:

    Warning: strpos(): Empty delimiter. in main on line 12
    I whole heartedly admit that I know nothing about php and would be grateful if someone would tell me where I've gone wrong.

    #2
    I'm confused. Is $nCount meant to be count of the number of words added so far? If so, then it isn't going to work because you don't increment it.

    The error shows that you're getting an empty value in $sWord, which is surprising. The foreach should only be giving you the individual words.

    I would avoid the error and tidy up the if like this:

    PHP Code:
            if ((strlen($sWord) > 0) && (strpos($sShort$sWord) === false))
                
    $sShort .= $sWord " "
    I'm not a PHP expert.

    Comment


      #3
      This code is overly complicated and prone to error, try this function as a better example:

      PHP Code:
      #GC: shorten to words
      if (!function_exists('ShortenByWords')) {
      function 
      ShortenByWords($string=''$chars=20$elli='...'){
      list(
      $new_string$elli)= explode("\n"wordwrap($string$chars"\n"false));
      return ( 
      $elli ) ? $new_string.'...' $new_string;
      }
      }

      $sShort "...";
      $nCount 50;
      $sOriginal "some string"

      echo 
      ShortenByWords($sOriginal,$nCount,$sShort); 

      Comment


        #4
        Thanks guys, will have a go and post back.

        Comment

        Working...
        X