2017-12-07 3 views
0

안녕하세요! 몇 가지 SQL 문이 있고이를 분할해야합니다. 문에서 발생하는 첫 번째 이미지 태그가 필요합니다. 분할 할 코드가 있지만 문제는 모두 다른 따옴표입니다. 그것을 밖으로 나가는 방법이 있습니까?php 값이 따옴표가있는 배열에서 벗어납니다

코드 지금이 :

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":""}'); 

난 그냥

원하는 :

$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"; 
} 
가 나는 배열에 몇 문이

올바른 문자열과 닮지 않는 따옴표로 인해 오류가 발생합니다. 누구든지이 방법을 알고 있습니까?

+0

구문이 잘못되어 구문 오류가 표시되지 않습니다. 문자열을 정의하고 문자열 내에서 동일한 따옴표를 사용해서는 안됩니다. 이중 따옴표를 사용하여 문자열과 작은 따옴표를 묶거나 내부의 단일 외부 및 이중 내부를 사용하십시오. 또는 내부 따옴표를 이스케이프 처리하십시오. – aynber

+0

글쎄, 네, 알아요.하지만 문제는 제 성명에 큰 따옴표가 들어있는 json 문자열이 있다는 것입니다. – Brum

답변

1

SQL 구문 분석은 간단한 작업이 아닙니다. (어려운 것도 아닙니다.) 일회성 일이라면, 당신이 원하는 것을 꺼내기 위해 몇 가지 정규식을 없앨 수 있습니다. 그렇지 않으면 파서를 사용해야합니다.

Regex /<img[^>]+>/은 이미지 태그를 추출합니다. 다양한 엣지 케이스에서 실패 할 것이므로 여기에 의존하지 말고 출력을 확인하십시오. PHP를 필요로하지 않습니다.

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

이것은 실제로 말 앞에 카트를 넣고 있습니다. 왜 작업을위한 도구를 사용하지 : Mysql? 삽입을 실행 한 다음 데이터를 쿼리하십시오. 끝난. mysql을 편리하게 사용하고 싶지 않다면, sqlite를 시도해보십시오. 그것은 당신의 SQL을 읽을 수 있어야합니다 것 같습니다.

+0

음, 이미지를 쉽게 열 밖으로 가져올 수 있습니까? 그것은 일회성 작업 일뿐입니다. 이전 데이터베이스를 새 데이터베이스로 가져오고 JSON 내부의 텍스트 필드에 이미지가 필요합니다. 그래서 이미지를 가져와 이미지에 추가 할 수있게되었습니다. JSON – Brum

+0

모든 이미지 태그를 추출하는 grep 명령을 추가했습니다. 그게 너에게 효과가 있니? – sba

관련 문제