Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 删除“(“和”)下的所有文本_Php - Fatal编程技术网

Php 删除“(“和”)下的所有文本

Php 删除“(“和”)下的所有文本,php,Php,我有一张桌子 |----------|-----------------------------------------|------------------| | id | name | desc | |----------------------------------------- ----------|------------------| | 123 | Empire

我有一张桌子

|----------|-----------------------------------------|------------------|
|    id    |        name                             |    desc          |
|----------------------------------------- ----------|------------------|
|    123   |   Empire : Kill (Kill everybody now)    |    desc 1        |
|----------------------------------------------------|------------------|
|    243   |   Witch : Show (Bloodthirst part 2)     |    desc 2        |
|----------------------------------------------------|------------------|
我正在编写以下代码以从表中提取数据

$columns_total  = mysql_num_fields($result); 

while ($row = mysql_fetch_array($result)) //retrieving each row 
    {
        for ($i = 0; $i < $columns_total; $i++) 
        {
            /*appending each column data into the row of the data 
                encapsulated within double quotes and separated by a comma */
            $output .='"'.$row["$i"].'",';
        }
    }
但是,我必须检查$row[$I]是name列还是desc列的值。 如果是“名称”列,则括号下的文本将被删除,否则不会。
我怎样才能做到这一点呢?

您可以通过以下方式将其分解:

$format_array = explode(":",$data);
$format_data = $format_array[0];
echo $format_data.':';
摆脱空间

$data = trim($data);

您可以分解字符后面的代码,然后使用修剪功能删除结尾处的空格:

$input = "Empire : (Kill everybody now)";

$input2 = explode("(",$input);

$input3 = trim($input2[0]);
现在,$input3变量保存了您要查找的结果。

通过使用mysql\u num\u fields方法,您将获得一个编号数组中的行。这意味着访问$row数组时指定的索引将决定要访问的列。要实现这一点,您有两种解决方案:

您可以根据正在使用的查询生成静态对应列\u name->index。如果$result变量包含类似于select*from表的内容,则索引将是数字1

您可以使用mysql\u list\u fields方法获取字段列表


所以一旦你得到了哪个索引是你的人,你只需要做一个简单的条件,比如$row==1{trim;},你就完成了

使用mysql\u fetch\u assoc而不是mysql\u fetch\u数组。

还有其他方法,但这可能是最简单的方法。为什么它们一开始就这样储存?最好将这些块分成多列。就像previuos一样,用户已经说您存储的数据不正确。如果需要拆分字符串,可以尝试查找then:字符,然后仅显示字符串的第一部分,即使输出匹配,从概念上讲,您的解决方案是错误的。你不是在移除和之间的东西,你只是移除了所有超出范围的东西。Anthony D'Andrea建议的解决方案对您的客户来说是正确的question@Bartserk检查我的问题到底是什么??我想知道$row[$i]是name列的值还是desc列的值…@Saswat我正在写一个答案。你刚刚编辑了这个问题;我想要类似于if$key=='name'的东西,因为数据将来自表1或表2,它们具有相同的列名,但索引不同。。。我甚至不能使用mysql\u fetch\u assoc。。。因为我需要使用for循环和counter,那么您肯定需要方法mysql\u list\u字段。将其存储到$variable中,并像$variable[1]=“name”一样访问它。但是如果表1中的$variable[1]可以成为name,而表2中的$variable[4]可以成为name呢??
$input = "Empire : (Kill everybody now)";

$input2 = explode("(",$input);

$input3 = trim($input2[0]);