V
V
Vadim Sverdlik2020-06-18 12:25:09
WordPress
Vadim Sverdlik, 2020-06-18 12:25:09

How can I change this function so that it cuts off the url and the separator is a dash?

I want to cut the url to 3 words when creating posts. This function cuts normally, but the words are separated by spaces, and the URL in WordPress is one whole, that is, words. connected dashes
localhost:8888/ipn/showbusiness/http-localhost8888...

How to put in this function so that it counts words and takes dashes as a word separator

function wph_cut_by_words($maxlen, $text) {
$len = (mb_strlen( $text) > $maxlen)? mb_strripos(mb_substr($text, 0, $maxlen), ' ') : $maxlen;
$cutStr = mb_substr($text, 0, $len);
$temp = (mb_strlen($text) > $maxlen)? $cutStr. '...' : $cutStr;
return $temp;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2020-06-18
@nokimaro

function wph_cut_by_words($maxlen, $text) {
    $len = (mb_strlen($text) > $maxlen) 
        ? mb_strripos(mb_substr($text, 0, $maxlen), '-') //тут "-" вместо " "
        : $maxlen;

    $cutStr = mb_substr($text, 0, $len);
    $temp = (mb_strlen($text) > $maxlen) ? $cutStr. '...' : $cutStr;
    return $temp;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question