0

I have a string of length 50k approx. example of string

hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl OR hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl OR hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl OR kdjkdjkdjkj OR djdldkkldkkkldklddklkldkl OR jdkdkjdjkjkdjkdjkdjkjkjkjkdkjkdjkjkdjkjk OR hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl hdjhjdhjdhdhjdhjhdldkklkdklkllkldkldldkl kldkldkl OR llldlkdl dkld ldk

Now if i use explode('OR', $str);, it will gives me array of length 500 or more than 501.

But i need array of 10 only. last array may be long. Does anybody have idea. how to do this.. I need OR word within string itself. also i have 20k OR in a string

Thanks

Rashmi
  • 551
  • 2
  • 4
  • 17
  • 1
    What should the 10 values of the array contain? Is there a logical place to split them, or do you want them split by size? – mavrosxristoforos Sep 25 '13 at 10:48
  • A rough even split would be to split by 'OR' as you're doing, then count the resulting elements returned. Divide it by 10, then loop through the array creating a new one, joining (el count / 10) elements together. If that makes any sense... – MDEV Sep 25 '13 at 11:22
  • If you have 20k+ occurances of OR in your string, chances are that, unless the rest of the string is not random, at some point you'll encounter an issue with something like _"foobar lalala OR new werther's ORIGINAL recipe OR not"_. Using `explode` will cause the string _"new werther's ORIGINAL recipe"_ to be split into _"new werther's "_ and _"IGINAL recipe"_ – Elias Van Ootegem Sep 25 '13 at 11:41
  • @EliasVanOotegem: I think he has the substring "20k OR" in his string and he just wants to split by "OR". – Alix Axel Sep 25 '13 at 12:17
  • @AlixAxel: Nope, check his first comment to my answer: _"i have 20k OR in a string. it is dividing into 20k"_, which would be impossible if the string were _"20k OR"_, given the expression I'm using, and the `PREG_SPLIT_DELIM_CAPTURE` flag – Elias Van Ootegem Sep 25 '13 at 12:38
  • @EliasVanOotegem: I think I'm right. I think he meant to say, the string is *splitting at* 20k. If what you are suggesting was the case, then the expressiong "also i have" wouldn't make much sense, nor would it produce an "array of length 500 or more than 501". – Alix Axel Sep 25 '13 at 13:42
  • @AlixAxel: Well, there's only 1 person who can tell us who's right... I'm still assuming the delimiter is `OR`, and the _20k_ is the `substr_count`. Given that: _"a string of length 50k"_, and _"[`explode`] gives me array of length 500 or more"_... However, I do believe the OP is having an issue with the subscrint _"OR"_ that shouldn't be treated as delimiter, and he's unable to distinguish between then (that's why I suggested at least to use `\bOR\b`). But an answer like yours only fixes 1 _very_ specific case of this problem... – Elias Van Ootegem Sep 25 '13 at 14:07
  • 1
    @EliasVanOotegem: I don't know anymore. I have posted 2 answers BTW, but this is confusing enough to make me not wanting to care anymore. – Alix Axel Sep 25 '13 at 14:24

3 Answers3

3
str_split($str, strlen($str) / 10);
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • you don't use the OR delimiter this way. – gmaliar Sep 25 '13 at 10:49
  • @Guy: I see no reference him wanting that in his question. – Alix Axel Sep 25 '13 at 10:50
  • It's in the last line of his question. – gmaliar Sep 25 '13 at 10:51
  • @Guy: I see what you're saying (taking the `explode('OR', ...)` as implicit, but I think it's ambiguous enough that I could take that he really wants to split into 10 chunks and `OR` was a failed attempt at doing so. `OR` appears nowhere else besides that `explode` (which is not working as he wants). – Alix Axel Sep 25 '13 at 10:56
  • what happens when `strlen($str)/10` returns a `float`? PHP docs don't supply any data about that. – gmaliar Sep 25 '13 at 10:59
  • @Guy: str_split will cast it into a integer. – Alix Axel Sep 25 '13 at 11:01
  • I checked it quick, it casts it into an `Int` and `floor`'s it. – gmaliar Sep 25 '13 at 11:02
  • @Guy: It doesn't actually floor the number, it just gets rid of the decimal part. Might look like it's the same but isn't: `abs((int) (7 / 2)) == abs((int) (-7 / 2))` – Alix Axel Sep 25 '13 at 11:10
2

This should work

explode('OR', $string, 10);

The third argument is the maximum number of array items in the array, the last one will hold the rest of the string remaining.

If you do need the OR inside your strings, you should use @Alix Axel's answer instead.

gmaliar
  • 5,294
  • 1
  • 28
  • 36
  • 1
    This does almost word for word what OP was looking for -- from the docs: "If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string." – brbcoding Sep 25 '13 at 10:51
  • can we make all array equal. – Rashmi Sep 25 '13 at 10:58
  • This will work, except in some (less likely) scenario's as I explained in my answer (and in the comment to the question: _"WERTHER's >ORROOR – Elias Van Ootegem Sep 25 '13 at 11:48
0

Here you go (demo):

preg_split('~(?<!\b20k\b) OR\b~', $str, 10);

I suggest next time, you state your whole, complete problem clearly.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500