-2

I inherited some php code for a webgame that I am trying to modify. I came across this line, and I can't figure out what it is supposed to be doing. Could someone help me out? $notice is just a regular string.

$notice = preg_replace("#([\S]{60})#i", "\\1 ", $notice);
KBriggs
  • 1,220
  • 2
  • 18
  • 43
  • * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Jul 13 '13 at 21:48
  • Please learn the regex for us! regular-expressions.info – Casimir et Hippolyte Jul 13 '13 at 21:50
  • [**StackOverflow**](http://bit.ly/4Agih5) is **NOT !**, a place to ask someone for free `codes`. [Such Questions are **Not Good** for this site](http://bit.ly/dcqznq), and will be [**Closed**](http://bit.ly/18T95z1), or [**Deleted**](http://bit.ly/10c3VuR), *Instead* [Learn what type](http://bit.ly/r0ZSEc) of questions you can or should ask. If you have any question about this, feel free to ask on [Meta](http://bit.ly/SgO5J), Or check the [FAQ](http://bit.ly/18T95z1), page for general information. – samayo Jul 13 '13 at 21:54
  • @Cyborgx37 Thanks. Never thought it was aggressive. I will try to update it. – samayo Jul 13 '13 at 23:21
  • Fair enough, I suppose I should have put some more work into researching it. My apologies. I will use the resources you have linked for future inquiries. – KBriggs Jul 14 '13 at 02:48

1 Answers1

7

It will find any continuous sequence of 60 non-whitespace characters in $notice, and insert a space after it:

  • (..) creates a capture group. Because it's the first group it's referred to as \1 in the replacement string. Because the whole pattern is in the group it's not really needed here.
  • [..] create a character class, but because it contains only one meta-character, it's not really needed here, either.
  • \S matches any non-whitespace character
  • {60} is a quantifier; it means 'repeated 60 times'.

This code is equivalent to:

$notice = preg_replace("#\S{60}#i", "\\0 ", $notice);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331