воскресенье, 30 октября 2011 г.

Regular expression to ignore escaped quotes within quotes

Params:
Language: PHP

Problem:
I have string:
$text = '[[["my tree \",and to and \"\" something \'\"else","moe derevo \" , a takzhe i\" \"nechto \" drugoe",""]],,"en",,[["en"]],2]';
I need only quoted part of it:  my tree \",and to and \"\" something \'\"else


Solution:

$pattern = '/"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"/imsu'; // Use this regular expression
$match = '';
preg_match($pattern, $text, $match);
var_dump($match[1]);

And you will see: my tree \",and to and \"\" something \'\"else

Комментариев нет:

Отправить комментарий