Answer the question
In order to leave comments, you need to log in
Automating the JSON Creation Process
Hello. In general, I have the following set of text.
Petrov Vasily Ivanovich
Neurologist
+7 (925) 123 45 67
[email protected]
Candidate of Medical Sciences, Physician of the 7th Department of Neurooncology
{
"name": "Petrov Vasily Ivanovich",
"profession": "Neurologist",
"description": "Candidate of Medical Sciences, Doctor of the 7th Neuro-Oncological Department",
"phone": "+7 (925) 123 45 67",
" email":"[email protected]"
},
Answer the question
In order to leave comments, you need to log in
Umm, well, just parse line by line and convert to JSON, what's the difficulty?
Here is an example if there are empty fields:
$str = " Петров Василий Иванович
+7 (925) 123 45 67
[email protected]
Кандидат медицинских наук, врач 7 Нейроонкологического отделения
";
$pattern = '/(.*)\n(.*)\n(.*)\n(.*)\n(.*)\n/';
$replacement = '{
"name":"$1",
"profession":"$2",
"description":"$5",
"phone":"$3",
"email":"$4"}';
echo preg_replace($pattern, $replacement, $str);
Here is a regex in php as an example:
$str = "
Петров Василий Иванович
Невролог
+7 (925) 123 45 67
[email protected]
Кандидат медицинских наук, врач 7 Нейроонкологического отделения
";
$pattern = '/(.+)\n(.+)\n(.+)\n(.+)\n(.+)\n/';
$replacement = '{
"name":"$1",
"profession":"$2",
"description":"$5",
"phone":"$3",
"email":"$4"}';
echo preg_replace($pattern, $replacement, $str);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question