in php, how do i truncate a string by x characters starting from the end? [closed]

烈酒焚心 提交于 2019-12-13 09:22:17

问题


How do I truncate a string thats 120 characters to 100 characters but have the count of the length start from the end of the string? So that the 20 characters are removed from the beginning of the string rather than the end?


回答1:


$newString = substr($oldString, -100) will return the last 100 characters of a string (or false if the string is shorter).




回答2:


Use substr i.e.

$newline = substr($oldline, 20);

or

$newline = substr($oldline, -100);



回答3:


You could use the substr function

$str = "Hello How are you?"
$str2 = substr($str, 5); // "How are you?"

For first 20 characters

$string = substr($str,20);



回答4:


Simply use substr in PHP
http://php.net/manual/en/function.substr.php

substr("abcdef", 20); // "abc.." is of course an example string. 
                      // this sets it to position start of 20 from left.



回答5:


substr()

$shortString = substr($string, 20);



回答6:


Let me suggest to start from the end. If you know already that is going to be 100 chars:

   $string=substr($yourstring,-100);


来源:https://stackoverflow.com/questions/15674534/in-php-how-do-i-truncate-a-string-by-x-characters-starting-from-the-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!