试图创建一个省略号,该省略号首先只显示字符串的一部分,然后显示单击函数后字符串的其余部分。
找到了许多创建ellispis函数的教程,但尝试了很长时间如何从结尾部分获得整个单词。
我试过这样做
<?php
$text="Lorem ipsum dolor sit amet.";
// 123456789
echo substr($text,0,9); // result: "Lorem ips"
echo '<hr>';
$start = substr($text,0,9);
// now this preg_replace() is awesome cause its only returning the entire word
echo preg_replace('/'w+$/','',$start); //result: "Lorem"
echo '<hr>';
echo substr($text,9,strlen($text)); //result: "um dolor sit amet."
// now how should this preg_replace be to get result "ipsum dolor sit amet."
?>
所以问题是:应该如何使用这个preg_replace()
来得到结果"ipsum dolor sit amet."
。
我已经尝试过更改类似preg_replace('/'$+w/','',$start);
的内容,但我不知道如何编写正则表达式。
preg_replace('/^'w+'s/','',$text)
Sorbos对我的问题的回答完全正确。由于我的问题不是很清楚,我不得不改变答案才能得到我需要的结果。问题是字符串可能从任何地方(给定的位置)开始。所以我仍然不知道这是否可以用preg_replace()解决
这给了我同样的结果:
$count=9;
$rest = substr($text,$count,strlen($text));
while( substr($rest, 0,1)!=' ' ) {
$rest = substr($text,$count,strlen($text));
$count--;
}
echo $rest;
如果有人有更好的解决方案,请随时发布。非常感谢。