使用 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> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry'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 'Content here, content here', 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 'lorem ipsum' 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> </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 "de Finibus Bonorum et Malorum" (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, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
【问题讨论】:
按换行符分解字符串***.com/a/3997367/5808894 【参考方案1】:如果用<br>
或<br />
划分行,则可以使用regex:
!! preg_split('/<br[^>]*>/i', $article->articleDescription)[0] !!
!! preg_split('/<br[^>]*>/i', $article->articleDescription)[1] !!
【讨论】:
我的描述中没有 标签。我现在已经编辑了以上是关于使用 laravel 从数据库中存在的大文本中获取少量文本的主要内容,如果未能解决你的问题,请参考以下文章
从 Laravel 数据库查询结果中访问明确存在的整数类型数据时出现错误“无法使用 stdClass 类型的对象作为数组”
Laravel - 从一个表中获取不存在于另一个表中的记录,并附加了 where 子句