【问题标题】:i want to use a part of mysql result of one row php我想使用一行php的mysql结果的一部分
【发布时间】:2015-05-03 00:59:54
【问题描述】:

我的表中有一些数据在单个like--

“了解了印度社会的实际情况以及 Maharishi Dayanand Saraswati 在其一生中所承担的任务的性质,他的追随者决定纪念他的生平和作品,而不是通过建造没有生命的雕像,而是通过开设寺庙学习 - 将 Maharishi 所倡导的所有价值观灌输给孩子们的学校和学院,以便他们能够“

我只需要第一行数据的一部分

“盘点印度社会和自然的基本现实”

有什么建议吗?

【问题讨论】:

  • 提取字符串的标准是什么,字母的数量,直到某个单词?
  • 您究竟是如何确定要参加哪个部分的?

标签: php mysql database mysqli


【解决方案1】:

如果没有更多细节,我可以建议最好的是以下 SQL 语句,它将从查询中检索前 X 个字符。

SELECT SUBSTRING( `collumnName`, 1,20) FROM `tableName`

或者,以下 SQL 查询将返回表列中的第一句。

SELECT SUBSTRING( `collumnName`, 1,LOCATE(".",`collumnName`)) FROM `tableName`

下图是这个查询在小型“书”数据库上工作的示例。

【讨论】:

  • 你能在单词边界上打破它吗?
  • Locate() 基于子字符串工作,因此您可以通过编辑来破坏任何您想要的位置。"到你想要的任何边界。
  • 老实说,这两种方法各有优缺点。将正则表达式与 PHP 一起使用将更容易定义在何处添加断点,因此如果存在复杂的断点但 OP 未指定在何处确定断点,您的解决方案可能是最好的。 :(
  • 我认为最好的子字符串中的最后一个空格。
  • 谢谢大家.. $sql = 'SELECT name,substring(details,1,50) as details FROM news where name="news" && time="1430613319"';
【解决方案2】:

确定大概的字符数。在您的示例中,它是 72 个字符。让我们找到 70 个字符后的第一个空格。

$text = '"Taking a stock of the ground realities of Indian society and the nature of the task undertaken by Maharishi Dayanand Saraswati during his life time, his followers decided to commemorate his life and works not by building lifeless statues, but by opening temples of learning - schools and colleges where all the values advocated by Maharishi would be inculcated in the children so that they could';

$last = strpos($text,' ',70);
$text = substr($text,0,$last);
echo "<p>$text</p>";

结果:

盘点印度社会和自然的基本现实

您还可以在一系列字符之间拆分多个字符:

$last = max(strpos(substr($text,0,90),' ',70),
strpos(substr($text,0,90),',',70),
strpos(substr($text,0,90),'.',70),
strpos(substr($text,0,90),';',70));

【讨论】:

    猜你喜欢
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 2017-03-09
    • 1970-01-01
    相关资源
    最近更新 更多