【问题标题】:How to take some specific word from string in php?如何从php中的字符串中获取一些特定的单词?
【发布时间】:2025-12-13 14:10:01
【问题描述】:

我在 mysql 中有一个包含两列或字段的表,一列用于id_column,其他用于字符串类型的description。我想在description 列中插入一个特定的字符串。

例如:

insert into table_name (id, description) values ('1','we go to school')

然后在我的 php 文件中,我想从 description 列中获取特定的词 "school"

我该怎么做?

【问题讨论】:

  • 你要取第三个字吗?那个字面意思是“学校”?这里的要求并不明确。
  • 你想在所有描述中取第 4 个单词吗?
  • 我想取我之前确定的特定词。在这种情况下,我决定我会选择“学校”这个词。所以单词学校最终会采取......
  • 你将如何确定这个词?你知道单词会存在吗?
  • 可能是LIKE Clause 会帮助你:tutorialspoint.com/mysql/mysql-like-clause.htm

标签: php mysql


【解决方案1】:

您可以将 like 与 wildchar 一起使用。

   select * from your_table 
   where description like '%school%';

在 php 中查找字符串是否包含可以使用的单词

$my_string = 'we go to school';

if (strpos($my_strong, 'school') !== false) {
   echo 'string found';
}

【讨论】:

  • 以及如何在 php 中仅使用“学校”一词?我需要单词 shool,而不是包含单词“school”的数据列。
【解决方案2】:

我不太明白你想要什么。但我想,你想从描述列中获取图像源。

$q = mysql_query("select id, description from tabel");
$row = mysql_fetch_assoc($q);

$html = $row["description"];

preg_match( '@src="([^"]+)"@', $html, $match );

$src = array_pop($match);

echo $src;

【讨论】:

    【解决方案3】:

    您需要使用 SQL Wildcard Characters

    所以你的查询可以是这样的:

    select * from tabel where description like '%school%';
    

    编辑:

    我想您需要先获取描述,然后根据它来管理输出。

    为此试试这个:

    $sqldata= mysqli_query('select * from tabel)';
    $rowcount=mysqli_num_rows($sqldata);
    if($rowcount > 0)
    {
        while($result = mysqli_fetch_assoc($sqldata)) // fetch records
        {
           $description = $result['desription'];
           $id = $result['id'];
        }
    }
    

    【讨论】:

    • 以及如何在 php 中仅使用单词“school”?我需要单词 shool,而不是包含单词“school”的数据列。
    • @RuslanWahyudi,你的意思是说你需要匹配那个词或检查字符串?或者 school 是动态的,而不是 we 也可以来
    • 请检查我的更新答案@RuslanWahyudi,但您的观点仍然不清楚需要做什么
    最近更新 更多