本文介绍了从数组中提取固定数量的字符,只需完整的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我在变量中有一个自定义文本:

Hey there, I have a custom text inside a variable:

$myvar = 'this is my custom text, it is a very long text so be patiente, take care!";

我需要显示让我说$ myvar中的前100个字符,但我需要完整的单词.例如:这是我的自定义文字,它是Ver ....(提取时我不想剪单词)

I need to display lets say the first 100 chars from $myvar, but i need full words.For example:this is my custom text, it is a ver... (i don't want to cut words when i extract)

我该怎么做?

谢谢!

推荐答案

有很多方法,但是请尝试以下方法:

There are heaps of ways, but try this:

$shortVersion = substr($myvar, 0, 100);
if(strlen($myvar)>100 && preg_match('`\w`', $myvar{100}))
    $shortVersion = preg_replace('`\w+$`', '', $shortVersion);

这可能是您抵抗力最小的途径.

That's probably your path of least resistance.

这篇关于从数组中提取固定数量的字符,只需完整的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 13:41