php使用引号从数组中获取值

时间:2017-12-07 11:59:44

标签: php mysql string insert

你好!我有几个SQL语句,我需要拆分它们,我需要在语句中出现的第一个图像标记,我有它的代码来拆分它,但问题是所有不同的引号。有没有办法把它拿出来?

我现在的代码:

$array = array('INSERT INTO ba793_content(`id`,`asset_id`,`title`,`alias`,`introtext`,`fulltext`,`state`,`catid`,`created`,`created_by`,`created_by_alias`,`modified`,`modified_by`,`checked_out`,`checked_out_time`,`publish_up`,`publish_down`,`images`,`urls`,`attribs`,`access`,`hits`,`metadata`) VALUES( 120333,139,'Champ Cars 2007','champ-cars-2007','<table style=width: 429px; height: 74px height=74 width=429 border=0><tbody><tr><td><img title=champcar_logo_2007.jpg style=width: 200px; height: 79px height=79 alt=champcar_logo_2007.jpg hspace=5 src=images/stories/Store/champcar_logo_2007.jpg width=200 align=left vspace=5 border=0 /></td><td><div align=justify>Exclusieve toegangskaarten alleen hier te koop! Wilt u tijdens het evenement op 2 september aanwezig zijn? kijk in mijn <a class=undefined href=index.php?option=com_content&task=view&id=94&Itemid=>shop</a> en bestel snel!</div></td></tr></tbody></table><hr />','',0,8,'2007-08-10 16:21:48',367,'','2008-04-17 15:09:13',367,0,'0000-00-00 00:00:00','2007-08-10 00:00:00','0000-00-00 00:00:00','{"image_intro":"images\/headers\/zonderdeur.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"images\/headers\/zonderdeur.jpg","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}','{"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}','{"article_layout":"","show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","info_block_show_title":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_associations":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_page_title":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}',1,0,'{"robots":"","author":"","rights":"","xreference":""}');,/* INSERT QUERY */INSERT INTO ba793_content(`id`,`asset_id`,`title`,`alias`,`introtext`,`fulltext`,`state`,`catid`,`created`,`created_by`,`created_by_alias`,`modified`,`modified_by`,`checked_out`,`checked_out_time`,`publish_up`,`publish_down`,`images`,`urls`,`attribs`,`access`,`hits`,`metadata`) VALUES( 120444,140,'Nieuwe helmen voor Nik en Koen','nieuwe-helmen-voor-nik-en-koen','<table border=0> <tbody> <tr> <td valign=top><img style=margin: 0px; width: 200px; height: 124px; border: 0px solid #000000; float: left; src=images/stories/Nieuws/helmen.jpg alt=helmen.jpg title=helmen.jpg height=124 width=200 /></td> <td valign=top>Speciaal uit Amerika zijn afgelopen winter twee nieuwe IRL Bell T6 Interceptors overgevlogen. Samen met de hoofdsponsor Eurogrip en Marcel Brouwers van Tjello Creations</td> </tr> </tbody> </table>','<p>is dit design tot stand gekomen en uiteindelijk super mooi gespoten op de helmen!<br /><br /></p>',1,8,'2008-04-17 15:08:48',367,'','2012-06-20 11:37:19',367,0,'0000-00-00 00:00:00','2008-04-17 14:23:23','0000-00-00 00:00:00','{"image_intro":"images\/headers\/zonderdeur.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"images\/headers\/zonderdeur.jpg","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}','{"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}','{"article_layout":"","show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","info_block_show_title":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_associations":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_page_title":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}',1,2088,'{"robots":"","author":"","rights":"","xreference":""}');');

foreach($array as $item) {

$split = preg_split('/src=/', $item);
$split = explode(' ', $split[1]);

echo $split[0] . "\n";
}

我在数组中有一些语句:

INSERT INTO ba793_content(`id`,`asset_id`,`title`,`alias`,`introtext`,`fulltext`,`state`,`catid`,`created`,`created_by`,`created_by_alias`,`modified`,`modified_by`,`checked_out`,`checked_out_time`,`publish_up`,`publish_down`,`images`,`urls`,`attribs`,`access`,`hits`,`metadata`) VALUES( 120333,139,'Champ Cars 2007','champ-cars-2007','<table style=width: 429px; height: 74px height=74 width=429 border=0><tbody><tr><td><img title=champcar_logo_2007.jpg style=width: 200px; height: 79px height=79 alt=champcar_logo_2007.jpg hspace=5 src=images/stories/Store/champcar_logo_2007.jpg width=200 align=left vspace=5 border=0 /></td><td><div align=justify>Exclusieve toegangskaarten alleen hier te koop! Wilt u tijdens het evenement op 2 september aanwezig zijn? kijk in mijn <a class=undefined href=index.php?option=com_content&task=view&id=94&Itemid=>shop</a> en bestel snel!</div></td></tr></tbody></table><hr />','',0,8,'2007-08-10 16:21:48',367,'','2008-04-17 15:09:13',367,0,'0000-00-00 00:00:00','2007-08-10 00:00:00','0000-00-00 00:00:00','{"image_intro":"images\/headers\/zonderdeur.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"images\/headers\/zonderdeur.jpg","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}','{"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}','{"article_layout":"","show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","info_block_show_title":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_associations":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_page_title":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}',1,0,'{"robots":"","author":"","rights":"","xreference":""}');,/* INSERT QUERY */
INSERT INTO ba793_content(`id`,`asset_id`,`title`,`alias`,`introtext`,`fulltext`,`state`,`catid`,`created`,`created_by`,`created_by_alias`,`modified`,`modified_by`,`checked_out`,`checked_out_time`,`publish_up`,`publish_down`,`images`,`urls`,`attribs`,`access`,`hits`,`metadata`) VALUES( 120444,140,'Nieuwe helme en en','nieuwe-helmn','<table border=0> <tbody> <tr> <td valign=top><img style=margin: 0px; width: 200px; height: 124px; border: 0px solid #000000; float: left; src=images/stories/Nieuws/helmen.jpg alt=helmen.jpg title=helmen.jpg height=124 width=200 /></td> <td valign=top>Speciaal uit Amerika zijn afgelopen winter twee niIRL Bell T6 Interceptors overgevlogen. Samen met de hoofip en Mers van Tjeions</td> </tr> </tbody> </table>','<p>is dit design tot stand gekomen en uiteindelijk super mooi gespoten op de helmen!<br /><br /></p>',1,8,'2008-04-17 15:08:48',367,'','2012-06-20 11:37:19',367,0,'0000-00-00 00:00:00','2008-04-17 14:23:23','0000-00-00 00:00:00','{"image_intro":"images\/headers\/zonderdeur.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"images\/headers\/zonderdeur.jpg","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}','{"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}','{"article_layout":"","show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","info_block_show_title":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_associations":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_page_title":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}',1,2088,'{"robots":"","author":"","rights":"","xreference":""}');

我只想要

<img style=margin: 0px; width: 200px; height: 124px; border: 0px solid #000000; float: left; src=images/stories/Nieuws/helmen.jpg alt=helmen.jpg title=helmen.jpg height=124 width=200 />

我现在得到的是错误,因为引号不会像正确的字符串,有人知道这样做吗?

1 个答案:

答案 0 :(得分:1)

解析SQL并非易事。 (这也不难。)如果这是一次性工作,你可能会拿出一些正则表达式来取出你想要的东西。否则你需要使用解析器。

正则表达式/<img[^>]+>/将提取图片代码。它会在各种边缘情况下失败,因此不要依赖于此并验证输出。而你不需要PHP:

egrep -o '<img[^>]+>' < old_db.sql

这真的把车推到马前。为什么不使用专门用于工作的工具:Mysql?运行插入,然后查询数据。完成。如果你没有方便的mysql,请尝试sqlite。它看起来应该能够读取您的SQL。