V
V
Vlad2018-01-07 21:58:23
PHP
Vlad, 2018-01-07 21:58:23

How to parse the desired part of the text?

Tell me, please, how to unload information about

tel-179->
your-email->

And the received information is placed in the form of an Excel spreadsheet.
_wpcf7->104
_wpcf7_version->4.7
_wpcf7_locale->ru_RU
_wpcf7_unit_tag->wpcf7-f104-p157-o3
_wpnonce->2b8273a977
your-name->Krisan Lang
menu-968->Firma
text-463->Best OÜ
tel-179- >58813468606
your-email->[email protected]
November 29, 2017, 18:07:14
_wpcf7->104
_wpcf7_version->4.7
_wpcf7_locale-> en_RU _wpcf7_unit_tag-
>wpcf7-f104-p67-o1
_wpnonce-3>
name->Liis dfValma
menu-968->Eraisik
text-463->
tel-179->5396643367
your-email->[email protected]

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Antonio Solo, 2018-01-07
@solotony

a) write a parser based on regular expressions
b) load into a text editor, sort, leave only the lines you need, copy them to the clipboard, open openOffice, paste (by specifying the separator ' -> '

A
Andrew, 2018-01-08
@mhthnz

The array will contain records of the form [0 => [], 1 => []] , it remains only to go through the array foreach and save it in the desired form (csv, tsv, ...)

<?php

function parse($text) {
    $lines = explode("\n", $text);
    $result = [];
    $switched = false;
    $i = 0;
    foreach($lines as $line) {
        
        // Add item
        if (count($matches = preg_split('/->/', $line, -1, PREG_SPLIT_NO_EMPTY)) == 2) {
            if (!isset($result[$i])) {
                $result[$i] = [];
            }
            $result[$i][$matches[0]] = $matches[1];
            $switched = false;
        } else if (!$switched) {
            $i++;
            $switched = true;
        }
    }
    return $result;
}

$text = "
_wpcf7->104
_wpcf7_version->4.7
_wpcf7_locale->ru_RU
_wpcf7_unit_tag->wpcf7-f104-p157-o3
_wpnonce->2b8273a977
your-name->Krisan Lang
menu-968->Firma
text-463->Best OÜ
tel-179->58813468606
your-email->[email protected]

November 29, 2017, 18:07:14
_wpcf7->104
_wpcf7_version->4.7
_wpcf7_locale->ru_RU
_wpcf7_unit_tag->wpcf7-f104-p67-o1
_wpnonce->2b8273a977
your-name->Liis dfValma
menu-968->Eraisik
text-463->
tel-179->5396643367
your-email->[email protected]
";


print_r(parse($text));

A
Anton Kiselyov, 2018-01-10
@zamboga

In your case, everything is done in a couple of clicks without scripts.
1. Insert your text into Excel
2. Split into columns, separator ">" (judging by the piece of text, this character is unique. If not, then first, through ctrl + a, replace "->" with some rare character that definitely does not exist in your text, for example, "No." Then do "separation by columns" by this character)
3. Apply a text filter on the first column, for "tel-" and "your-email"
4. Copy the filtered values ​​to a new sheet.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question