如何更新/删除一行文本中的单个单词 [英] How to update/delete a single word in a line of text

查看:87
本文介绍了如何更新/删除一行文本中的单个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题需要您的帮助.如何更新一组用逗号分隔的文本.当文本输入数据库时​​,它是一行文本,每个单词之间用逗号隔开,但是当我想将其回显时,我使用explode函数将它们分隔为单个单词,而不是像我一样将一行文本分隔开在数据库中.

I need your help with a slight problem I have. How can I update a portion of group of text separated by comma. When the text is entered into the database, it's a single line of text with commas between each words, but when I want to echo it out I use the explode function to separate them into individual words and not a single line of text like I have it in the database.

所以我现在的问题是,如何对数据库中的单个单词进行更新/删除.记住,我把它作为数据库中的一行文本,并且我对更新整行文本不感兴趣……只是文本中的一个单词.

So my question now is, how can I make an update/Delete to a single word in the database. Remeber, I have it as a single line of text in the database, and I'm not interested in updating the whole line of text...just a single word in the text..

谢谢.

$text = "name,fname,lname,class,age" ;    
$newtext = explode(",", $text) ;

推荐答案

更新或删除数组中的单词,然后插入数组并将新值保存到数据库字段中.

Update or remove the word from your array, then implode the array and save the new value to the database field.

//Remove all instances of $value from $array
function array_remove($array, $value) {
  return array_filter($array, function ($e) use ($value) {
      return $e != $value;
    });
}

// Add only unique values
function array_add($array, $value) {
  if (!in_array($value, $array))
    $array[] = $value;
  return $array;
}

$text = "name,fname,lname,class,age" ;
$newtext = explode(",", $text) ;
$newtext = array_remove($newtext, 'lname'); // Remove lname
$newtext = array_add($newtext, 'mail');     // Add mail
$newtext = array_add($newtext, 'class');    // Won't add it again
$newtext = implode(',', $newtext);          // name,fname,class,age,mail

这篇关于如何更新/删除一行文本中的单个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆