2

I know I have done this before. I am not sure why I am struggling to do something so simple.

All I am trying to do is add a single quote to a comma separated string.

The php process initially looks like this:

<?php
  $checknumber = mysqli_real_escape_string($dbc,trim($_POST['checknumber']));

 $splitnumber = preg_replace('/\n$/','',preg_replace('/^\n/','',preg_replace('/[\r\n]+/',"\n",$checknumber )));

 $numbers = "'" . implode("', '", $splitnumber ) ."'";

 echo $checknumber;
 echo $splitnumber;
 echo $numbers;
?>

The results look like this:

// $checknumber
AMD111111,AMD222222,AMD333333

// $splitnumber
AMD111111,AMD222222,AMD333333

// $numbers
''

I need the results of $numbers to look like this:

'AMD111111','AMD222222','AMD333333'

The funny thing is I am using a piece of code I've used before that works. So I am at a loss as to why the code is not working here.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89
  • Possible duplicate of: https://stackoverflow.com/questions/6102398/php-implode-101-with-quotes – dukedevil294 Apr 13 '18 at 20:49
  • `$splitnumber` is a string, which is an invalid argument type for `implode()`'s second argument, so that function returns NULL. So you effectively have `$numbers = "'" . NULL . "'";` which is `$numbers = "''";` P.S. PHP will be raising an E_WARNING message complaining about the invalid argument type, be sure to turn up your error_reporting level and to turn on displaying errors (or log them). – salathe Apr 13 '18 at 20:50
  • @salathe - How can I fix this? – John Beasley Apr 13 '18 at 20:52
  • You're putting quotes on your imploded string, instead of putting quotes on the individual splitnumber. – Daniel Apr 13 '18 at 20:53
  • It sounds like using the Implode is useless here. – John Beasley Apr 13 '18 at 20:53
  • 1
    @JohnBeasley it looks like you want `$splitnumber` to be an array like `array('AMD111111','AMD222222','AMD333333')`, so have a look at [`explode()`](http://php.net/explode). – salathe Apr 13 '18 at 20:54
  • Explode your database result. Do ya thing so the data is quoted and implode it. – Daniel Apr 13 '18 at 20:57
  • I'm just thinking what'll happen if and when a user inputs a quote; that stands to possibly break your code. – Funk Forty Niner Apr 13 '18 at 21:04

2 Answers2

4

The implode() function requires an array as second argument. It looks like your $splitnumber is just a simple string not an array of strings as it should probably be. Try splitting your $splitnumber by commas using the explode() function and put the result of that in the implode function.

Should look like that (not tested):

$splitnumber = ...;
$splittedNumbers = explode(",", $splitnumber);
$numbers = "'" . implode("', '", $splittedNumbers) ."'";

There is probably a cleaner solution by just replacing all occurences of , with ','.

Andi489156
  • 105
  • 10
  • This is it! This is what I was looking for. Sheesh. Such a simple task. I don't even want to say how long I've been working on this today. Thank you, sir. This has solved my problem. – John Beasley Apr 13 '18 at 21:00
-1

Your implode isn't setup correctly.

$numbers = "'" . implode("','", $splitnumber) . "'";
dukedevil294
  • 1,285
  • 17
  • 31
  • I did try that, but I only got the same results. I did not downvote you, my friend. – John Beasley Apr 13 '18 at 20:56
  • Your implode divider goes between the values. You would start without your first single quote and also will be without the last quote. – Daniel Apr 13 '18 at 20:58