1

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)
4
  • please post the other part of your codes (replaces everything but a-zA-Z0-9 and space with "") Commented Dec 3, 2020 at 23:29
  • @KenLee using this: $cleaned = preg_replace("([^a-zA-Z0-9])","",$str); Commented Dec 3, 2020 at 23:35
  • @syd You haven't put a space in that regex... Commented Dec 3, 2020 at 23:36
  • @Steven sorry. there are actually two lines. one replaces space, one doesn't. I posted the wrong line mistakenly. It was indeed a tab, that was causing the issue. Commented Dec 3, 2020 at 23:49

1 Answer 1

1

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.

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

1 Comment

Thanks. Needed to add an additional line to replace all tabs with space by trim(preg_replace('/\s+/', ' ', $ss));.

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.