1

I am attempting to write my first custom function. I understand that there are other functions out there that do the same thing that this does, but this one is mine. I have the function wrote, but I do not understand char_list as it pertains to functions and cannot figure out the third parameter of the str_word_count function in php. I think I need to this in a certain format to maintain periods, comma, semi-colons, colons, etc. Please note that double and single quotes are maintained throughout the function. It is bottom symbols that are strip from the string.

$text = "Lorem ipsum' dolor sit amet, consectetur; adipiscing elit. Mauris in diam vitae ex imperdiet fermentum vitae ac orci. In malesuada."

function textTrim($text, $count){ 
  $originalTxtArry = str_word_count($text, 1);

  $shortenTxtArray = str_word_count(substr(text, 0,$count), 1);
  foreach ($shortenTxtArray as $i=>$val) {
    if ($originalTxtArry[$i] != $val) {
      unset($shortenTxtArray[$i]);
    }
  }
  $shortenTxt = implode(" ", $shortenTxtArray)."...";
  return $shortenTxt;
} 

Output Lorem ipsum' dolor sit amet consectetur adipiscing elit Mauris in diam...

Notice the "," after amet is missing.

Ignore the string of periods at the end, I concatenate those on to the end before the return

Thank You for all assistance.

Dave

10
  • 1
    Sorry, but what is your function supposed to do? What input do you send to it, what's the expected output, and what do you actually get? Commented May 6, 2015 at 15:41
  • 1
    What's your question @Dave? Commented May 6, 2015 at 15:42
  • The third argument is any other valid characters that can be in a word. Look at the examples they provide, it seems pretty straight forward. They have a '3' in the word fri3nd, without '3' in the third argument, the word is considered 'fri'. Commented May 6, 2015 at 15:44
  • 1
    What is the purpose of this function, are you trying to limit the number of words? Commented May 6, 2015 at 15:46
  • 1
    If you don't want to worry about losing punctuation, then you could just simply explode on a space then join. Commented May 6, 2015 at 15:49

3 Answers 3

2

Updated function to explode based on a space

function textTrim($str, $limit){ 
    /** remove consecutive spaces and replace with one **/
    $str = preg_replace('/\s+/', ' ', $str);

    /** explode on a space **/
    $words = explode(' ', $str);

    /** check to see if there are more words than the limit **/
    if (sizeOf($words) > $limit) {
        /** more words, then only return on the limit and add 3 dots **/
        $shortenTxt = implode(' ', array_slice($words, 0, $limit)) . '...';
    } else {
        /** less than the limit, just return the whole thing back **/
        $shortenTxt = implode(' ', $words);
    }
    return $shortenTxt;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hey Man, that are errors all through that. But it gave me the right idea. I fixed everything. Will post answer in a minute
sorry about that, I didn't even test it. Updated function to resolve the syntax error.
I exclude the else state and just returned the implode function but they both have the right idea. Thanks for the assistance.
0

From the PHP manual about the third parameter, charlist:

A list of additional characters which will be considered as 'word'

These are any characters outside the usual a-z which should be included as part of a word, and not causing a word to break.

If you look at example 1 on the PHP manual you linked to, it will show an example where the word 'fri3nd' is only classed as 1 word when 3 is included in the charlist parameter.

2 Comments

Put how do I make it so that commas and periods are preserved throughout function
based on the function definition and documentation, you would set your third parameter to be ",." (quote-comma-period-quote)
-1
<?php
function trimTxt($str, $limit){ 
    /** remove consecutive spaces and replace with one **/
    $str = preg_replace('/\s+/', ' ', $str);

    /** explode on a space **/
    $words = explode(' ', $str);

    /** check to see if there are more words than the limit **/
    if (sizeOf($words) > $limit) {
       /** more words, then only return on the limit and add 3 dots **/
       $shortTxt = implode(' ', array_slice($words, 0, $limit)) . 'content here';
    } else {
       /** less than the limit, just return the whole thing back **/
       $shortTxt = implode(' ', $words);
    }
    return $shortTxt;
}
?>

1 Comment

Please don't just write the code, explain it to the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.