我想拆分一段......这是我的段落
一个,两个,三个灯笼,重量轻,但充满了 诱人的质感。三种不同但同时互补 形状,东西方平衡。一盏灯 在最多样化的环境中散发出轻松的光芒 宣纸或桑树皮的神奇之处 传统的日本灯笼,在珍贵的物理吸引力 吹过的和磨砂的玻璃。横切的横线 灯具的表面营造出柔和迷人的装饰 同时有助于过滤光源的强度, 将充足,温暖和柔和的光线散射到房间里。吹制的玻璃 身体 - 具有令人愉悦的石膏般的外观 - 悬挂在一个 基地由三个金属脚组成:一个非常有特色的特征 这盏灯似乎漂浮在半空中。它们非常适合 单独使用或以和谐的形状组合;在床边 桌子,控制台和桌子,甚至在地板上:对于一个区域 致力于放松或作为冥想灯,明智地照明两者 我们生活的空间以及我们的室内世界。
我希望它看起来像这样......
一个,两个,三个灯笼,重量轻,但充满了 诱人的质感。三种不同但同时互补 形状,东西方平衡。一盏灯 在最多样化的环境中散发出轻松的光芒 宣纸或桑树皮的神奇之处 传统的日本灯笼,在珍贵的物理吸引力 吹制和磨砂玻璃。
意味着从给定的段落...我想删除70个字标后的所有单词.... 我使用了php的Substr函数...但它总是计算字符....
我需要一种方法来计算单词并删除70个单词后面出现的额外单词....
答案 0 :(得分:0)
是的,你可以这样做。
使用str_word_count();
功能。 More info
示例:强>
在线demo。 (示例和演示有5个单词)
$string = 'One, two, three lanterns that are lightweight yet brimming with seductive texture.';
function shorten_string($oldstring, $wordsreturned){
$string = preg_replace('/(?<=\S,)(?=\S)/', ' ', $oldstring);
$string = str_replace("\n", " ", $string);
$array = explode(" ", $string);
if (count($array)<=$wordsreturned)
{
$retval = $string;
}
else
{
array_splice($array, $wordsreturned);
$retval = implode(" ", $array)." ...";
}
return $retval;
}
if(str_word_count($string, 0) > 5){ // if smaller then 5 words
echo shorten_string($string, 5).'...'; // we only want 5 words.
} else {
echo $string; // do nothing, show the string
}
注:
shorten_string()
的积分:https://stackoverflow.com/a/12444960/1501285
答案 1 :(得分:0)
你必须对此进行一些处理,然后你得到如下的ans: -
$limit = 5 // Change to 70 for your example
$str = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
$arr = explode(" ", str_replace(",", ", ", $str));
for ($index = 0; $index < $limit; $index++) {
echo $arr[$index]. " ";
}
它可能对你有帮助。
答案 2 :(得分:0)
迪帕克,
我认为你的70个字正在这里完成......水平线切割..
请尝试以下代码
$str="One, two, three lanterns that are lightweight yet brimming with
seductive texture. Three different yet concurrently complementary
shapes, balancing between the east and the west. A family of lamps
that sheds its relaxing light in the most diverse settings, conveying
the magic of rice paper or mulberry tree bark, the raw materials of
traditional Japanese lanterns, in the precious physical appeal of
blown and frosted glass.The horizontal lines which cut across the
surface of the lamps give rise to a soft and charming décor and at the
same time contribute to filtering the intensity of the light source,
diffusing a full, warm and soft light into the room. The blown glass
body - with its pleasantly plaster-like appearance - is suspended on a
base consisting of three metal feet: a highly distinguishing trait, on
which the lamp appears to be floating mid-air. They are perfect for
use alone or in a harmonious composition of shapes; on a bedside
table, a console and a table or even on the floor: for an area
dedicated to relaxation or as meditation lamps, wisely lighting both
the space we live in as well as our interior world.";
echo get_my_excerpt($str,70);
function get_my_excerpt($str,$length)
{
$str_segments=explode(" ",$str);
if(count($str_segments)>=$length)
{
$temp_array=array();
for($i=0;$i<$length;$i++)
{
$temp_array[]=$str_segments[$i];
}
return implode(" ",$temp_array);
}
else
{
return $str;
}
}
答案 3 :(得分:0)
使用此regex
模式将为您提供字符串中的前70个单词。
$str = 'One, two, three lanterns that are lightweight yet brimming with seductive texture. Three different yet concurrently complementary shapes, balancing between the east and the west.
A family of lamps that sheds its relaxing light in the most diverse settings, conveying the magic of rice paper or mulberry tree bark, the raw materials of traditional Japanese lanterns,
in the precious physical appeal of blown and frosted glass.
The horizontal lines which cut across the surface of the lamps give rise to a soft and charming décor and at the same time contribute to filtering the intensity of the light source,
diffusing a full, warm and soft light into the room.
The blown glass body - with its pleasantly plaster-like appearance - is suspended on a base consisting of three metal feet: a highly distinguishing trait, on which the lamp appears to be floating mid-air.
They are perfect for use alone or in a harmonious composition of shapes;
on a bedside table, a console and a table or even on the floor:
for an area dedicated to relaxation or as meditation lamps, wisely lighting both the space we live in as well as our interior world.';
preg_match('#^([^.!?\s]*[\.!?\s]+){0,70}#',$str,$matches);
print $matches[0];
输出
One, two, three lanterns that are lightweight yet brimming with seductive texture. Three different yet concurrently complementary shapes, balancing between the east and the west.
A family of lamps that sheds its relaxing light in the most diverse settings, conveying the magic of rice paper or mulberry tree bark, the raw materials of traditional Japanese lanterns,
in the precious physical appeal of blown and frosted glass.
The horizontal lines which
换行符是因为我的格式化,
而额外的水平线是因为如何计算到70个单词的工作原理:)
答案 4 :(得分:0)
<?php
$str = "One, two, three lanterns that are lightweight yet brimming with seductive texture. Three different yet concurrently complementary shapes, balancing between the east and the west. A family of lamps that sheds its relaxing light in the most diverse settings, conveying the magic of rice paper or mulberry tree bark, the raw materials of traditional Japanese lanterns, in the precious physical appeal of blown and frosted glass.The horizontal lines which cut across the surface of the lamps give rise to a soft and charming décor and at the same time contribute to filtering the intensity of the light source, diffusing a full, warm and soft light into the room. The blown glass body - with its pleasantly plaster-like appearance - is suspended on a base consisting of three metal feet: a highly distinguishing trait, on which the lamp appears to be floating mid-air. They are perfect for use alone or in a harmonious composition of shapes; on a bedside table, a console and a table or even on the floor: for an area dedicated to relaxation or as meditation lamps, wisely lighting both the space we live in as well as our interior world.";
$str = str_replace('.', '. ', $str);
function limit_text($text, $limit)
{
if (str_word_count($text, 0) > $limit)
{
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]);
}
return $text;
}
echo limit_text($str, 66);
?>