Search code examples
phparraysexplode

Explode is not working as it should when string fetched from database


I am trying to use explode on the product name saved in my database. I have a product called: A4Tech KR-83FN Multimedia Bangla USB Black Comfort Round Edged Keycaps Normal Keyboard in the database. when I fetch the entry using select query and use explode on it, it's not working properly, and getting the following array:

array (size=4)
  0 => string 'A4Tech' (length=6)
  1 => string 'KR-83FN' (length=7)
  2 => string 'Multimedia' (length=10)
  3 => string 'Bangla USB Black Comfort Round Edged Keycaps Normal Keyboard' (length=68)

But, when I am copying the text and using explode() on it, it's working properly.

array (size=12)
  0 => string 'A4Tech' (length=6)
  1 => string 'KR-83FN' (length=7)
  2 => string 'Multimedia' (length=10)
  3 => string 'Bangla' (length=6)
  4 => string 'USB' (length=3)
  5 => string 'Black' (length=5)
  6 => string 'Comfort' (length=7)
  7 => string 'Round' (length=5)
  8 => string 'Edged' (length=5)
  9 => string 'Keycaps' (length=7)
  10 => string 'Normal' (length=6)
  11 => string 'Keyboard' (length=8)

The code is mentioned below:

<?php
$conn = new mysqli("localhost", "root", "", "dbname");;
$sql = "select * from products where name like '%A4Tech KR-83FN Multimedia%'";
$result = $conn->query($sql);
$row = $result->fetch_row();
$ss = $row[5];
//$ss = "A4Tech KR-83FN Multimedia Bangla USB Black Comfort Round Edged Keycaps Normal Keyboard";
$exploded = explode(" ",$ss);
var_dump($exploded);

I think I am missing something simple and it's the only product name that's having this issue.

I have another part of code after it, which replaces everything but a-zA-Z0-9 and space with "", it shouldn't replace the space but that part actually removes every bit of space from the fourth element of the following array. So, space is not getting considered as space.

array (size=4)
  0 => string 'A4Tech' (length=6)
  1 => string 'KR-83FN' (length=7)
  2 => string 'Multimedia' (length=10)
  3 => string 'Bangla USB Black Comfort Round Edged Keycaps Normal Keyboard' (length=68)

Solution

  • If the code is working for every product in the database except for this one then it stands to reason that the error is with the format of this text.

    Usually my bet would be that the string contains tabs instead of spaces after the point where it stops working:

    A4Tech KR-83FN Multimedia Bangla USB Black Comfort Round Edged Keycaps Normal Keyboard
          ^       ^          ^      ^   ^     ^       ^     ^     ^       ^      ^
        {        Spaces         } {        Tabs                                     }
    

    In your case however the string should be 60 bytes and is actually 68 bytes (as per your question) so my bet would be that instead of tabs (single tabs would only bring it to 64 bytes) you have newline/carriage returns like \r\n.

    Seeing as it is only one product I strongly suggest simply changing the entry manually...

    Your update - the additional code replacing [^a-zA-Z0-9 ] - seems to confirm the theory.

    Of course it could be \t\t, \n\t, \n\n or any other combination of white space characters.