如何在 php 中从字符串中获取一些特定的单词


How to take some specific word from string in php?

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

例如:

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

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

我该怎么做?

你可以像 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';
}

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

$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;
您需要

使用 SQL 通配符

因此,您的查询可以是这样的:

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'];
    }
}