title: How to split up a string after a certain number of characters in PHP
body: <p>I am using PHP to return the user's browser user agent The problem is where I want to print it: I don't want the length to be longer than about 30 characters per line. Is there a way to break the returned variable (from the function that I call to get the string) into substrings of a certain length? And since UA strings are different lengths, I am not sure what to expect.</p>
  <p>This is the PHP code where I return the user agent:</p>
  <pre><code>function __toString() {
      return &quot;Browser Name:
      return &quot;Browser Name:             {$this-&gt;getBrowser()}  \n&quot; .
             &quot;    Browser Version:          {$this-&gt;getVersion()} \n&quot; .
               &quot;  Browser User Agent String:           {$this-&gt;getUserAgent()} \n&quot; .
             &quot;    Platform:                 {$this-&gt;getPlatform()} &quot;;
  }
  </code></pre>
  <p>In particular, this call <code>$this-&gt;getUserAgent</code>. I output using this:</p>
  <pre><code>&lt;?php require_once('browser.php'); $browser =  new Browser(); echo $browser . &quot;\n&quot;; ?&gt;
  </code></pre>
  <p>Right now, the name, version and platform calls output like I want to (because none are anywhere near as long at the UA string).</p>
  <p>So in short, how do I split up the returned user string so that it won't exceed a certain number of characters per line? Ideally, I'd like to store them into temporary variables, because I have to add spaces in between the words. For example, where it says &quot;Platform&quot;, there are spaces before it, so it lines up vertically with Browser Version, and then spaces so that the result of all the returned strings from the functions line up.</p>
  <p>In case anyone wants the Github code for above to see what I am doing, the function calls are in <a href="https://github.com/chriscct7/jigoshop/blob/dev/admin/browser.php" rel="nofollow noreferrer">this</a> on lines 339-243, and the echoed results go to <a href="https://github.com/chriscct7/jigoshop/blob/dev/admin/jigoshop-admin.php" rel="nofollow noreferrer">this</a> on line 152.</p>
  <h1>At this point I am very very close</h1>
  <h2>Just need help adding spaces before the wrapped text (see my answer below)</h2>
  <p>This is what I have right now:</p>
  <pre><code>$text1   = $this-&gt;getUserAgent();
  $UAline1 = substr($text1, 0, 26);
  
  $text2       = $this-&gt;getUserAgent();
  $towrapUA    = str_replace($UAline1, '', $text2);
  $wordwrapped = chunk_split($towrapUA, 26, &quot;\n\r&quot;);
  </code></pre>
  <p>The only issue at this point it how do I get a constant number of spaces before each of the wrapped code? I need (lets say) 20 spaces before all of the wrapped lines for formatting.</p>