我怎样才能抓住下面字符串的最后7个字符?
例如:
$dynamicstring = "2490slkj409slk5409els";
$newstring = some_function($dynamicstring);
echo "The new string is: " . $newstring;
Run Code Online (Sandbox Code Playgroud)
哪个会显示:
The new string is: 5409els
Run Code Online (Sandbox Code Playgroud) 如何使用php从字符串中获取前5个字符
$myStr = "HelloWordl";
Run Code Online (Sandbox Code Playgroud)
结果应该是这样的
$result = "Hello";
Run Code Online (Sandbox Code Playgroud) 我有一个用PHP编写的代码片段,它从数据库中提取一个文本块并将其发送到网页上的小部件.原始文本块可以是冗长的文章或短句或短句; 但对于这个小部件,我不能显示超过200个字符.我可以使用substr()来切断200个字符的文本,但结果会在单词中间切断 - 我真正想要的是在200个字符之前在最后一个单词的末尾剪切文本.
我的MySQL数据库中有一个描述字段,我在两个不同的页面上访问数据库,一个页面显示整个字段,但另一方面,我只想显示前50个字符.如果说明字段中的字符串少于50个字符,那么它将不会显示...,但如果不是,我将在前50个字符后显示....
示例(完整字符串):
Hello, this is the first example, where I am going to have a string that is over 50 characters and is super long, I don't know how long maybe around 1000 characters. Anyway this should be over 50 characters now ...
Run Code Online (Sandbox Code Playgroud)
例2(前50个字符):
Hello, this is the first example, where I am going ...
Run Code Online (Sandbox Code Playgroud) 我知道如何使用substr函数,但是这将很快结束一个单词的中间字符串.我希望字符串在一个单词的结尾处结束我将如何进行此操作?它会涉及正则表达吗?任何帮助非常感谢.
这就是我到目前为止所拥有的.只是SubStr ......
echo substr("$body",0,260);
Run Code Online (Sandbox Code Playgroud)
干杯
我试图在字符串过滤器中使用此方法:
public function truncate($string, $chars = 50, $terminator = ' …');
Run Code Online (Sandbox Code Playgroud)
我期待这个
$in = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890";
$out = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV …";
Run Code Online (Sandbox Code Playgroud)
还有这个
$in = "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ??????????????????????????????";
$out = "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ?????????????????? …";
Run Code Online (Sandbox Code Playgroud)
这是$chars减去$terminator字符串的字符.
此外,滤波器应该在$chars极限以下的第一个字边界切割,例如
$in = "Answer to the Ultimate Question of Life, the Universe, and Everything.";
$out = "Answer to the Ultimate Question of Life, the …";
Run Code Online (Sandbox Code Playgroud)
我很确定这应该适用于这些步骤
但是,我现在尝试了各种组合str*和mb_*功能,但都产生了错误的结果.这不是那么困难,所以我显然缺少一些东西.有人会为此分享一个有效的实现,或者指向一个资源,我终于可以理解如何做到这一点.
谢谢
PS是的,我之前已经检查过https://stackoverflow.com/search?q=truncate+string+php :)
可能重复:
PHP - 在X个字符后剪切一个字符串
有没有办法根据特定大小剪切或修剪字符串.
假设我们有"Lorem Ipsum dolor",我们希望将该字符串剪切为恰好适合20 Px的宽度,并在字符串的末尾添加"...".
到目前为止我试过这个:http://www.php.net/manual/en/function.imagettfbbox.php但很难得到结果作为PHP的新手.
我需要获取给定字符串的第一个字符.这里我在会话变量中有一个名字.我将变量值传递substr给获取字符串的第一个字符.但我不能.
我想得到该字符串的第一个字符.
例如John doe.我想得到字符串的第一个字符j.我怎么能用PHP获取它?
<?php
$username = $_SESSION['my_name'];
$fstchar = substr($username, -1);
?>
Run Code Online (Sandbox Code Playgroud) 我需要接收一个包含大量字符的长字符串,然后将其“剪切”以生成一个具有我确定的字符数的字符串,我该怎么做?
例子:
$text = 'This is a long string with a lot of characters';
Run Code Online (Sandbox Code Playgroud)
$text在此示例中,该字符串包含 46 个字符。
我需要生成一个$newText仅包含前 20 个字符的字符串,如下所示:
$newText = 'This is a long strin';
Run Code Online (Sandbox Code Playgroud)