使用 laravel 从数据库中存在的大文本中获取少量文本

Posted

技术标签:

【中文标题】使用 laravel 从数据库中存在的大文本中获取少量文本【英文标题】:Get small amout of text out of large text present in database using laravel 【发布时间】:2018-07-09 06:42:50 【问题描述】:

在我的 laravel 项目中,我有一个名为 description(datatype:Text) 的数据库列。

我只想提取两行文本。

我如何在 laravel 中实现这一点。

我需要借助正则表达式吗?

 <p>!!  $article->articleDescription !! </p><br>

这就是我的文本在数据库中的样子。

    <h2>What is Lorem Ipsum?</h2>

<p><strong>Lorem Ipsum</strong>&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

<h2>Why do we use it?</h2>

<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using &#39;Content here, content here&#39;, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for &#39;lorem ipsum&#39; will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>

<p>&nbsp;</p>

<h2>Where does it come from?</h2>

<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of &quot;de Finibus Bonorum et Malorum&quot; (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, &quot;Lorem ipsum dolor sit amet..&quot;, comes from a line in section 1.10.32.</p>

【问题讨论】:

按换行符分解字符串***.com/a/3997367/5808894 【参考方案1】:

如果用&lt;br&gt;&lt;br /&gt; 划分行,则可以使用regex:

!! preg_split('/<br[^>]*>/i', $article->articleDescription)[0] !!
!! preg_split('/<br[^>]*>/i', $article->articleDescription)[1] !!

【讨论】:

我的描述中没有 标签。我现在已经编辑了

以上是关于使用 laravel 从数据库中存在的大文本中获取少量文本的主要内容,如果未能解决你的问题,请参考以下文章

从 Laravel 数据库查询结果中访问明确存在的整数类型数据时出现错误“无法使用 stdClass 类型的对象作为数组”

Laravel - 从一个表中获取不存在于另一个表中的记录,并附加了 where 子句

通过索引 Laravel 5 从集合中获取项目

Laravel 从 textarea 获取每一行并插入数据库中的新行

从固定宽度的大文本中解析未排序的数据

ajax获取数据请求url未找到但已经存在,Laravel 8