Search code examples
phparraysregexstrpospdftotext

How to count similar words from array with strpos function of php?


I am using xpdf to convert pdf to text and then with help of regex function seraching for words after colon in pdf and then looping that data with strpos function of php and storing them into database.Its working for me for single data. but for multiple same data i am not getting how to add this data into database.

step by step i will show you my code and response:

i am using xpdf to convert my pdf into text format with below code.

$text1 = (new Pdf('C:\xpdf-tools-win-4.00\bin64\pdftotext.exe'))
->setPdf($pathoffile)
->setOptions(['layout', 'layout'])
->text();
$string = $text1;

On echo $string i am getting data i.e:

                                 In respect of Shareholders

Name:                                    xyz

Residential address:                     dublin

No of Shares:                            40

Name:                                    abc

Residential address:                     canada

No of Shares:                            2

SO, i am getting total 2 shareholders with my above data. Now i want to store this data in my table shareholders.

Now, i am using preg_match_all function to convert this data into array and then storing into databaase.

$array = array('Name','Residential address','No of Shares');
preg_match_all($regex, $string, $matches);

Using below function for getting array with strpos().

function strposa($haystack, $needles=array(), $offset=0) {
        $chr = array();
        foreach($needles as $needle) {
                $res = strpos($haystack, $needle, $offset);
                if ($res !== false) $chr[$needle] = $res;
        }
        if(empty($chr)) return false;
        return min($chr);
}

if($this->strposa($text1, $array) !== false) 
                {
                    foreach ($matches as  $value) {
                        //print_r($matches);
                        $value=array_map('trim',$value);
                        $directors_info->name= $value[0];
                        $directors_info->address= $value[1];
                        $directors_info->shares= $value[2];
                      }
                }

My array of print_r($matches) shows me data :

[0] => Array
        (
            [0] =>              xyz
            [1] =>              dublin
            [2] =>              40
            [3] =>              abc
            [4] =>              canada
            [5] =>              2
        )

but if i am getting multiple shareholders then it will not work for me. My expected output would be:

[0] => Array
            (
                [0] =>              xyz
                [1] =>              dublin
                [2] =>              40
                [0] =>              abc
                [1] =>              canada
                [2] =>              2
            )

My concern is to store 2 data separately with 2 rows in table. How to achieve this kind of data. thanks in advance.


Solution

  • You can not have duplicate keys, so you could create a multidimensional array. If the data for each row is always there, you could use array_chunk with a size of 3:

    $matches = array_chunk($matches,3);
    

    That would give you:

    Array
    (
        [0] => Array
            (
                [0] => xyz
                [1] => dublin
                [2] => 40
            )
    
        [1] => Array
            (
                [0] => abc
                [1] => canada
                [2] => 2
            )
    
    )
    

    Demo