substr 和 strlen 解释

发布于 2025-01-03 09:19:56 字数 964 浏览 0 评论 0 原文

好的,伙计们,这个问题与我上一个问题有关。

如果我设置了 $textlimit = 500; 这会将我的文本限制为 500 个字符。

有没有办法“避免”文本限制,然后 onclick 函数加载其余部分?

例如,如果我设置:

$textpart = substr($fulltext, 0, 400);

$textpart 将仅包含 400 个字符的字符串。

我的问题是,如何声明变量,该变量将包含长度超过 500 个字符的文本的其余部分?

变量示例:

$fulltext //Contains full text, but is limited to 500 characters.
$textpart //Contains part of the text, substr 400 characters out of 500.
$textrest //This variable to "hold" rest of the text, after 400 characters of $textpart.

就像我在上一个问题中所问的那样,我想制作展开和折叠按钮,我现在知道如何做到这一点,但我不知道如何划分文本。

表单将如下所示:

  1. 这里随机文本(400 个字符长)
  2. 用于展开的随机图像
  3. 声明 onclick 函数 I 后,加载文本的其余部分(超过 500 个字符)。
  4. 用于崩溃的随机图像
  5. 在声明 onclick 函数崩溃并返回到之前的状态之后 - 引文 1.

我希望我以正确的方式解释了我的问题。我真的很感激任何类型的帮助,如果我可以选择,我只想了解如何做到这一点的基本解释,因为我想学习这一点,而不是复制/粘贴解决方案(它更容易,但我不会学到太多)。

提前致谢。

Ok guys, this question is related to my previous one.

If I have set $textlimit = 500; that will limit my text to 500 characters.

Is there any way to "avoid" text limit, and then onclick function load rest of it?

For example, if I set:

$textpart = substr($fulltext, 0, 400);

$textpart will only contain 400 characters of string.

My question is, how to declare variable, which will contain the rest of the text which is much longer than 500 characters?

Example of variables:

$fulltext //Contains full text, but is limited to 500 characters.
$textpart //Contains part of the text, substr 400 characters out of 500.
$textrest //This variable to "hold" rest of the text, after 400 characters of $textpart.

Like I've asked in previous question, I wanted to make expand and collapse button, I now know how to do that, but I don't know how to divide text.

Form would go like this:

  1. Random text here(400 characters long)
  2. Random image for expand
  3. After declared onclick function I, load rest of the text (Over 500 characters).
  4. Random image for collapse
  5. After declared onclick function collapse and return to previous state - citation 1.

I hope I explained my question the right way. I would really appreciate any kind of help, if I can choose, I would like just basic explanation on how to that, because I want to learn that, not copy/paste solution (it is easier, but I will not learn much).

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好听的两个字的网名 2025-01-10 09:19:56
$textrest = substr($fulltext, 400)
$textrest = substr($fulltext, 400)
凉墨 2025-01-10 09:19:56
$fulltext = substr($fulltext, 0, 500);
$textpart = substr($fulltext, 0, 400);
$textrest =  substr($fulltext,400,strlen ( $fulltext ));
$fulltext = substr($fulltext, 0, 500);
$textpart = substr($fulltext, 0, 400);
$textrest =  substr($fulltext,400,strlen ( $fulltext ));
献世佛 2025-01-10 09:19:56

如果我理解正确,您希望向用户显示一个仅显示前 X 个字符的初始页面,然后在用户单击文本时显示所有字符。

有三种策略可以做到这一点。从简单到困难:

  1. 输出缩短的文本并包含一个将重新加载整个页面但包含整个文本的链接
  2. 输出所有文本并使用 css 和 JavaScript 隐藏/显示任何溢出
  3. 输出缩短的文本并执行 Ajax 调用来加载额外的字符和附加

选项 2 和 3 需要使用客户端 JavaScript,因此不是纯 PHP 解决方案。

选项 1 是将 $_GET 变量(例如 ?expand=para1)添加到您的 url 中,并扩展 PHP 中通过 $_GET[' 标识的文本展开']

不要错误地认为 PHP 仍在浏览器的页面上运行。只有 JavaScript 可以在浏览器中运行网页。 (我知道这并不完全正确,但在现实中足够真实。)

If I understand you correctly you want to show the user an initial page that shows only the first X characters and then show all the characters when the users clicks on the text.

There are three strategies to do this. From easy to hard:

  1. Output the shortened text and include a link that will reload the whole page but with the whole text
  2. Output all the text and use css and JavaScript to hide/show any overflow
  3. Output the shortened text and perform an Ajax call to load the extra characters and append

Options 2 and 3 require the use of client side JavaScript and are therefore not pure PHP solutions.

Option 1 is a matter of adding a $_GET variable, e.g. ?expand=para1, to your url and expanding the text identified in PHP by $_GET['expand'].

Do not make the mistake of thinking PHP is still running on the page in the browser. Only JavaScript can run in the browser on the web page. (Not strictly true I know, but true enough in reality.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文