-1

I have list of strings like this:

'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'

It looks like elements of an array, But when i use

<?php
    $string = "'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'";
    $arr = array($string);
    print_r($arr);

 ?>

It doesnt work as I want:

Array ( [0] => 'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys')

Instead of:

Array ( [0] => PYRAMID, [1] => htc_europe, [2] => htc_pyramid, ...

I dont want to use explode() because my strings are already in array format and many strings have the ',' character. Please help me, thanks.

David Mcintyre
  • 100
  • 1
  • 12

4 Answers4

5

Your string is not in an array format. From the way it looks and based on your comments, I would say that you have comma separated values, CSV. So the best way to parse that would be to use functions specifically made for that format like str_getcsv():

$str = "'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'";

// this will get you the result you are looking for
$arr = str_getcsv($str, ',', "'");

var_dump($arr);

The use of the second and third parameters ensures that it gets parsed correctly also when a string contains a comma.

jeroen
  • 91,079
  • 21
  • 114
  • 132
0

$string is still a string, so you explode it if you want to make an array out of it.

If your problem is strings have the ',' character, use some other seperator, maybe |

   $string = "'PYRAMID'|'htc_europe'|'htc_pyramid'|'pyramid'|'pyramid'|'HTC'|'1.11.401.110 CL68035 release-keys'|'htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'";
    $arr = explode('|',$string);
    print_r($arr);
Muhammet
  • 3,288
  • 13
  • 23
  • 1
    "I dont want to use explode() because my strings are already in array format. Please help me, thanks." Well done for reading his question. – Dan Belden Jun 28 '15 at 10:04
  • @Dan Belden His string is of type `string`, not of type `array`. The OP's question is incorrect. – kulaeff Jun 28 '15 at 10:07
  • Irrelevant, the question specifically says no explode - he has now updated to justify why also. "Many of my strings contain a ,". This solution is invalid and was a rush answer without reading the question. Thus... down vote. – Dan Belden Jun 28 '15 at 10:11
  • Removed my down vote, however I believe @jeroen's answer using str_getcsv() is the perfect solution to this problem. Thanks for updating anyway :) – Dan Belden Jun 28 '15 at 10:20
  • @DanBelden Yes I absolutely agree :) – Muhammet Jun 28 '15 at 10:20
  • this will work also if the user have access to the string generation. It can't be used if the string is as-is and can't be modified – Gianmarco Jun 28 '15 at 10:31
-1
<?php
$int = preg_match_all(
    "/'(.+?)'/",
    "'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'",
    $matches);
print_r($matches[1]);

You can test it here http://micmap.org/php-by-example/en/function/preg_match_all

mcorne
  • 77
  • 2
-1

Due to the edits in the question, my answer is now out of date. I will leave it here because it contains a little explanation why in a particular case explode will be a valid solution.

as you can read in the manual online of php, there is a very precise syntax that can be used when creating an array, this is the reference:

http://php.net/manual/en/function.array.php

As you can see the correct way to use array() to create a new array is declaring each value separated by a comma or by declaring each pair index => value separated by a comma.

There is -no way- to pass a single string to that method (I see it something json like in javascript or java maybe, but this is Off Topic) simply because it won't parse it, the method will take the whole string as is and of course putting it into a single index (that in your case will be index zero).

I am telling you of course to use explode() or split() or to parse your string before, and what I told you before is the reason to my statement.

You probabily want to have each single model of phone in a string inside the array so you will have to remove the single quote first:

$stringReplaced = str_replace("'", "", $yourString);

And then you will have to split the string into an array using:

$array = explode(',',$yourString);

I hope you will take this in consideration

Of course as told by my collegue up there, you can treat this string as a comma separated value and use str_getcsv.

~Though you will need to remove the single quotes to have the pure string.~ (last statement is wrong because you can use the enclosure char param provided by str_getcsv)

Gianmarco
  • 2,536
  • 25
  • 57
  • 1
    This wont work as some of his strings contain commas. – Dan Belden Jun 28 '15 at 10:15
  • are you sure? I couldn't see any piece that contains a comma – Gianmarco Jun 28 '15 at 10:19
  • I believe he has only posted a sub-set of his string, I'd hazard a guess the string is longer with more words... and he was likely after a more resilient solution. Nice try though, love explode! :) – Dan Belden Jun 28 '15 at 10:21
  • 1
    thank you. What I thought was that the kind of data inside tha string will ,with high probability, not contain any comma. I can see an IP address, an URL and some phone model names. This is what I based my answer on, of course if the string it's more complex and contains also elements with commas inside, my method is broken. – Gianmarco Jun 28 '15 at 10:25