1
post_title
B Project Kodou Ambitious 0101 Boys Meet Girl
B Project Kodou Ambitious 0102 Boys Meet Girl
B J and the Bear 0110 Lobo
B J and the Bear 0111 Crackers
B O R N To Style 0101 Homeless Boy To Rock Star Glam
B O R N To Style 0101 Homeless Boy To Rock Star Glam

I have this csv file which have these type of strings in each cell. I want to seperate the text before 4 digits in each string. And I also want 4 digits in each string. Here is the code that I came up with so far.

$file = fopen('e.csv', 'r');

while (($row = fgetcsv($file)) !== FALSE) {
  $csv[] = $row;
}

$new_csv = array();
foreach ($csv as $row)
{
  $n = count($csv[0]);
  for ($q = 0; $q < $n; ++$q)
  {
    $new_row[$csv[0][$q]] = $row[$q];
    $new_csv[] = $new_row;
  }

  $title1 = ($new_row['post_title']); 
  $a_title = explode(" " ,$title1);
  $tot_a = count($a_title);

  $i = 0;
  foreach ($a_title as $session_episode) {
    $session_episode1 = preg_match_all('!\d!', $session_episode, $matches);
    if ($session_episode1 != 0 && $session_episode1 != '' && $session_episode1 == 4) {
      $j[] = $i;
    }
    $i++;
  }
  $len = $j[0];
  $a_title[$len];
  echo '<pre>' . var_dump($a_title[$len]) . '</pre>';
}

In this code, it does iterate the 4 digit from each string, but only for first 2 cells. I am new to php. Can anyone help me with how I can get 4 digits from each string with these csv rows. Thanks in advance

1
  • 1
    How is each cell separated, looks like everything is just spaces from what you posted... ? Commented Jan 22, 2018 at 18:00

1 Answer 1

1

As you want text before digit,digit and then text after digit in an array. Do like below-

<?php
    $array = [];
    if (($handle = fopen("e.csv", "r")) !== FALSE) {
        $i = 0;
        while (($data = fgetcsv($handle, 1000, " ")) !== FALSE) {
            $result = array();
            $index = 0;
            foreach ($data as $key=>$number) {
                if (is_numeric($number)) {
                    $index++;
                    $result[$index][] = $number;
                    $index++;
                }
                $result[$index][] = $number;
            }
            unset($result[2][0]);
            $array[] = [implode(' ',$result[0]),implode(' ',$result[1]),implode(' ',$result[2])];
        }
        fclose($handle);
    }
    echo "<pre/>";print_r($array);
?>

Output at my local end:-

Array
(
    [0] => Array
        (
            [0] => B Project Kodou Ambitious
            [1] => 0101
            [2] => Boys Meet Girl
        )

    [1] => Array
        (
            [0] => B Project Kodou Ambitious
            [1] => 0102
            [2] => Boys Meet Girl
        )

    [2] => Array
        (
            [0] => B J and the Bear
            [1] => 0110
            [2] => Lobo
        )

    [3] => Array
        (
            [0] => B J and the Bear
            [1] => 0111
            [2] => Crackers
        )

    [4] => Array
        (
            [0] => B O R N To Style
            [1] => 0101
            [2] => Homeless Boy To Rock Star Glam
        )

    [5] => Array
        (
            [0] => B O R N To Style
            [1] => 0101
            [2] => Homeless Boy To Rock Star Glam
        )

This is how my CSV looks:-

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

@JaiMadhwan glad to help you :):)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.