在进入MYSQL之前从字符串中删除特定文本

时间:2012-04-16 10:52:13

标签: php mysql arrays insert

大家好我有一个PHP数组来自rss feed有2组数据。标题和描述。

(数组的打印示例,我想编辑数组中的所有描述项而不仅仅是一个索引)

[2] => ('Remembrance','Release Date: Thursday 19th April 2012')

在进入mysql表之前,如何操作描述字符串以删除“Release Date:”?

这是我的其他代码:

$rss = simplexml_load_file('rss.xml');
$title = $rss->xpath('//title');  //finding the title tags in the xml document and loading into variable
$description = $rss->xpath('//description');

$rows=array();

foreach($result as $title){
  $rows[]="('".mysql_real_escape_string($title)."','".mysql_real_escape_string(array_shift($description))."')";
}
mysql_query("INSERT INTO Films (Film_Name, Film_Release) VALUES ".implode(',',$rows));
print_r($rows);

2 个答案:

答案 0 :(得分:2)

你是str_ireplace不区分大小写的

示例

$rows = array ();
$array = array (); // So many array
$array [2] = array (
        'Remembrance',
        'Release Date: Thursday 19th April 2012' 
); // Sample array Value

foreach ( $array as $detail ) {
    $title = $detail [0];
    $desc = str_ireplace ( "Release Date:", "", $detail [1] );
    $desc = rim($desc);
    $rows [] = sprintf ( "('%s','%s')", mysql_real_escape_string ( $title ), mysql_real_escape_string ( $desc )   );
}

mysql_query("INSERT INTO Films (Film_Name, Film_Release) VALUES ".implode(',',$rows));
print_r($rows);

答案 1 :(得分:1)

如果您只需要删除“发布日期:”字符串,请使用str_replace('Release Date: ', '', 'Release Date: Thursday 19th April 2012');

如果您的数据是动态的,请使用preg_replace()preg_replace('#^[^\:] #', '', 'Release Date: Thursday 19th April 2012');

相关问题