php省略号,以preg_替换开头和结尾的完整单词

php省略号,以preg_替换开头和结尾的完整单词,php,regex,preg-replace,Php,Regex,Preg Replace,尝试创建一个省略号,只显示字符串的一部分,并在单击函数后显示字符串的其余部分 找到了许多创建ellispis函数的教程,但花了很长时间尝试如何从结尾部分获取整个单词 我试过这样做 <?php $text="Lorem ipsum dolor sit amet."; // 123456789 echo substr($text,0,9); // result: "Lorem ips" echo '<hr>'; $start =

尝试创建一个省略号,只显示字符串的一部分,并在单击函数后显示字符串的其余部分

找到了许多创建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\u replace('/\$+w/','$start)但我不知道如何编写正则表达式。

Sorbos的答案完全符合我的问题。因为我的问题不是很清楚,所以我不得不改变答案以得到我需要的结果。问题是字符串可能从任何位置(给定位置)开始。所以我仍然不知道这是否可以用preg_replace()解决

preg_replace('/^\w+\s/','',$text)
这给了我同样的结果:

$count=9;
$rest = substr($text,$count,strlen($text));
while( substr($rest, 0,1)!=' ' ) {
    $rest = substr($text,$count,strlen($text));
    $count--;
}
echo $rest;
如果有更好的解决方案,请随时发布。
谢谢大家!

太棒了,谢谢!你有任何链接或资源来学习如何编写这些正则表达式吗?@caramba:要学习正则表达式,这是最好的教程之一: