从内容中选择前90个字符

时间:2016-06-14 06:23:40

标签: php wordpress

我正在修改一段代码,其实质是从帖子的正文中挑选前90个字符。我设法得到包含一些标点字符的文本。

我的问题是我不知道如何获得90个字符而不是忽略换行符。我希望它在遇到换行符时终止。就像现在一样,它不尊重它,因此最终添加来自另一行/段的内容。

这是我正在使用的代码 -

$title_data = substr($postdata,0,90);
$title_data = preg_replace("/[^\w#&,\":; ]/",'', strip_tags($title_data));
$data['post_title'] = "F. Y. I - " . $title_data . " ...";

3 个答案:

答案 0 :(得分:1)

执行preg_replace()的第一步,然后将该值放入substr()参数。

$title_data = preg_replace("/[^\w#&,\":; ]/",'', strip_tags($postdata));
$data = substr($title_data,0,90);
$data['post_title'] = "F. Y. I - " . $data . " ...";

答案 1 :(得分:0)

有点复杂,但可能有助于获得您所追求的结果:

// Use `wpautop` to work WP's paragraph-adding magic.
$rawText = wpautop($postdata);

// Remove all the opening `<p>` tags...
$preSplitContent = str_replace('<p>', '', $rawText);
// ...and then break into an array using the closing `</p>` tags.
// (hacky, but this gives you an array where each
// item is a paragraph/line from your content)
$splitContent = explode('</p>', $preSplitContent);

// Then run your preg_replace
// (because `$splitContent[0]` is only the first
// line of your text, you won't include any content
// from the other lines)
$firstLine = preg_replace("/[^\w#&,\":; ]/",'', strip_tags($splitContent[0]));

// Then trim the result down to the first 90 characters
$finalText = substr($firstLine,0,90);

$data['post_title'] = "F. Y. I - " . $finalText . " ...";

答案 2 :(得分:0)

这是我的分数...它还确保单词不被截断。

select * from user_errors
where user_id = '2'
and ((type = 'custom_type' and second_user_id != '2') or second_user_id = '2')