php - 为什么 substr(...) 没有做我想要它做的事?

标签 php mysql wordpress wordpress-theming

我有一段代码可以在我的网站上生成职位列表的预览。它应该是不言自明的,但我试图在我的数据库中获取类型为 VARCHAR (5000) 的列的前 100 个字符。

            $Jobs = $wpdb->get_results('SELECT * FROM jobs');
            foreach ($Jobs as $thisJob)
            {
                // use first 100 characters of job description before a "Read more" link to the full description
                echo  ' <div class="job-row">'
                      .   '<h3 class="job-title">' . $thisJob->title . '</h3>'
                      .   '<div class="job-descr-brief">'
                      .        substr($thisJob->descr, 100) . ' <a href="' . get_site_url . '/about/careers/?jobid=' . $thisJob->id . '">Read more.</a>'
                      .   '</div>'
                      .'</div>';
            }

然而,出于某种原因,substr($thisJob->descr, 100) 返回完整的 $thisJob->descr 字符串,而不仅仅是前 100 个字符.知道我在这里做错了什么吗?

相关文档:http://www.w3schools.com/php/func_string_substr.asp

最佳答案

我认为您误读了文档。

你需要像这样使用它来获取前 100 个字符:

substr($thisJob->descr, 0, 100);

第二个参数是您希望它开始的位置(在本例中为 0,开始)。

关于php - 为什么 substr(...) 没有做我想要它做的事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32104427/

相关文章:

php - 将 MySQL 数据库中的表显示到数组中

php - 我可以有条件地更改 Wordpress 中 the_content() 返回的内容吗?

php: in_array() 与阿拉伯语

php - 我需要验证信用卡详细信息

php - AJAX 轮询 - 检查新的数据库条目

MySql 聚合计算

php - WooCommerce 自定义循环按名称而不是 Slug

php - 为什么我有 WooCommerce 订单,但没有 shop_order 类型的帖子?

php - 如何重用 var name = document.getElementById ("name");在同一文档中多次

css - WordPress Bootstrap 。不向右流动