L
L
leha782015-10-19 09:33:18
PHP
leha78, 2015-10-19 09:33:18

How to competently replace a character using preg_replace?

INSERT INTO "accounts_order"("id","name","email","address","phone","comment","retail_price","wholesale_price","retail_price_with_discount","status","created_at","updated_at","user_id","guid") VALUES ( 3,' Имя','[email protected]','12мк','+12345667',"Comment",'31582','18160','31582','success','2015-09-30 09:32:56.495034','2015-09-30 11:04:49.227136',1,'3b5a4db4-6756-11e5-a891-9' );

Using preg_replace, I need to replace the double quotes with ` between INSERT INTO and VALUES, do not touch after VALUES (double quotes are also found there).
If you specify in the pattern that it is replaced between INSERT INTO and VALUES, then the entire pattern, including INSERT INTO and VALUES, is replaced by `

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Optimus, 2015-10-19
Pyan @marrk2

some nonsense but:

preg_match("/INSERT INTO(.*?)VALUES/ism", $text, $match)
Теперь замените в match все нужные кавычки на одинарные а потом сделайте str_perlace в главной строке и всё.

A
Alexey Skobkin, 2015-10-19
@skobkin

Are you sure that you are not fighting the symptoms instead of treating the disease? Because if you need to replace something in SQL queries using regular expressions, it is most likely better to rewrite their construction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question