-1

I am Involve a CodeIgniter Project.In this project another developer insert cat_id in the database include # separated like #1#. when insert two cat_id the value store in database like #1##2#.

Now I want to retrieve the cat_id and join it to the another table.

  • Are you understand my problem ? – Minhazur Rahaman Feb 20 '19 at 10:46
  • I want to retrieve 1 and 2 form #1##2# . I don't change the database or insert code – Minhazur Rahaman Feb 20 '19 at 10:53
  • "I want to retrieve 1 and 2 form #1##2# . I don't change the database or insert code" Converting your code and database to support normalization is alot faster then you trying to figure this one out, trust me you don't to got in the direction off using extra code to parse out the cat_id from #1##2# – Raymond Nijland Feb 20 '19 at 11:09

3 Answers3

0

Ok, I've made a mistake in my first attempt (find bellow), this hopfully this should work:

$text = "#1##2##10000";
$ids = array_filter(explode('#', $text));

// output
array(3) { [1]=> string(1) "1" [3]=> string(1) "2" [5]=> string(5) "10000" }

Bonus if you want to reset the array keys to start from 0 again:

$ids = array_values(array_filter(explode('#', $text)));

// output
array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(5) "10000" }

Old incorrect answer:

$text = "#1##2#";

$ids = [];
for ($i=0; $i < strlen($text); $i++) { 
    if ($text[$i] != '#') {
        $ids[] = $text[$i];
    }
}

var_dump($ids);
// produced result
array(2) { [0]=> string(1) "1" [1]=> string(1) "2" }
failedCoder
  • 1,346
  • 1
  • 14
  • 38
0

You can try this,

$str = "#1##2#";
preg_match_all('|\d+|',$str,$matches);
print_r($matches);

Here is working demo.

Rahul
  • 18,271
  • 7
  • 41
  • 60
  • The general way would be to init the `$matches` array `$matches = array(); $matches = []; (PHP5-7)` first before using `preg_match_all(...)` to avoid warnings and possible flowcontrol bugs later in the code about missing the `$matches` variable – Raymond Nijland Feb 20 '19 at 11:22
  • Please show me that error reporting level in PHP where it will throw warning/notice for undefined variable for matches. `By Documentation of preg_match_all`. As per my knowledge, `$matches` is getting defined there. – Rahul Feb 20 '19 at 11:33
  • "As per my knowledge, $matches is getting defined there." Now i indeed look better it's `&$matches` which is a reference so it will not generate missing warnings.. Ok i will give you a other reason (less important) then missing out possible IDE code auto completion support on the `$matches` variable.. Also the general sense would be to init the variable always also when the variable is autoset by a reference like in this case then you follow good programming habits in mine opion. – Raymond Nijland Feb 20 '19 at 11:44
  • Okay @RaymondNijland I will keep a note of it. I always stick to basics, `E_ALL` is always active while I do code. Thanks. – Rahul Feb 20 '19 at 11:49
  • what about the $matches @RahulMeshram – Minhazur Rahaman Feb 20 '19 at 17:55
-1

I want to retrieve 1 and 2 form #1##2# . I don't change the database or insert code

I strongly suggest you into changing your PHP code and MySQL tables into supporting normalisation.
Because the needed query to fetch ("parse") a cat_id number from #1##2# is a total nightmare and will not perform well.
The query looks more or less like this without the extra join to a other table.

SELECT 
  DISTINCT
   REVERSE(
      REVERSE(
     SUBSTRING_INDEX(SUBSTRING_INDEX(<table>.<column>, '##', rows.row), '##', -1) 
   ) >> 1 ) AS cat_id
FROM (
  SELECT 
   @row := @row + 1 AS ROW
  FROM (
    SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
  ) row1
  CROSS JOIN (
    SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
  ) row2  
  CROSS JOIN (
    SELECT @row := 0 
  ) init_user_params
)
 ROWS
CROSS JOIN 
 <table>

But then i feel the next question coming.

How can i convert that SQL into codeignitor's code?

So now a PHP example to parse out cat_ids

PHP code

<?php
  $text = "#1##2##10000";

  $cat_ids = explode("##", trim($text, '#'));

  var_dump($cat_ids);
?>

Result

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(5) "10000"
}
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
  • A mobile application is running using the database. So I can't any change this DB. And yes it hard from me. and you are right about my next question. – Minhazur Rahaman Feb 20 '19 at 11:20
  • "A mobile application is running using the database" @MinhazurRahaman I assume after hearing that you didn't build in a application version checker which blocks and forces the user into downloading a mobile application update? – Raymond Nijland Feb 20 '19 at 11:28
  • "And yes it hard from me. and you are right about my next question" If mine SQL code works you should only have cat_id's you still have to make a join.. Codeignitor supports `$this->db->query('YOUR QUERY HERE');` [manual](https://www.codeigniter.com/userguide3/database/queries.html) to direct query access keep in mind the SQL in there can have SQL injections when user input is used so you need to use `$this->db->escape(..)` to protect those.. This is the most easy method. – Raymond Nijland Feb 20 '19 at 11:31