2

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.

This is the PHP code where I return the user agent:

function __toString() {
    return "Browser Name:
    return "Browser Name:             {$this->getBrowser()}  \n" .
           "    Browser Version:          {$this->getVersion()} \n" .
             "  Browser User Agent String:           {$this->getUserAgent()} \n" .
           "    Platform:                 {$this->getPlatform()} ";
}

In particular, this call $this->getUserAgent. I output using this:

<?php require_once('browser.php'); $browser =  new Browser(); echo $browser . "\n"; ?>

Right now, the name, version and platform calls output like I want to (because none are anywhere near as long at the UA string).

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 "Platform", 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.

In case anyone wants the Github code for above to see what I am doing, the function calls are in this on lines 339-243, and the echoed results go to this on line 152.

At this point I am very very close

Just need help adding spaces before the wrapped text (see my answer below)

This is what I have right now:

$text1   = $this->getUserAgent();
$UAline1 = substr($text1, 0, 26);

$text2       = $this->getUserAgent();
$towrapUA    = str_replace($UAline1, '', $text2);
$wordwrapped = chunk_split($towrapUA, 26, "\n\r");

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.

Community
  • 1
  • 1
Chris Mirno
  • 337
  • 5
  • 13
  • 3
    The function you're looking for is called [`wordwrap`](http://php.net/wordwrap). – hakre Aug 04 '12 at 16:44
  • possible duplicate of [How to Truncate a string in PHP to the word closest to a certain number of characters?](http://stackoverflow.com/questions/79960/how-to-truncate-a-string-in-php-to-the-word-closest-to-a-certain-number-of-chara) – hakre Aug 04 '12 at 16:45
  • @hakre But I dont want to truncate it. I want to keep the whole string just format it on return. – Chris Mirno Aug 04 '12 at 16:47
  • @hakre wordwrap looks promising though. – Chris Mirno Aug 04 '12 at 16:47
  • 1
    The wordwrap function is not truncating it. If the suggested duplicate does not fullfill your specific needs, I'm sorry for being bad at picking the right one, but I bet there are many duplicate questions of yours on this site at least. – hakre Aug 04 '12 at 16:48
  • [`str_split()`](http://php.net/str-split) or [`chunk_split()`](http://php.net/chunk-split) is what you want, I think... – DaveRandom Aug 04 '12 at 16:50
  • @hakre actually see the answer I posted below. I used your idea, and I am very close. I just can't figure out how to add spaces to do formatting. – Chris Mirno Aug 04 '12 at 16:58
  • @DaveRandom I am so close, can you help me with the answer I posted below. Just need help figuring out how to add spaces. – Chris Mirno Aug 04 '12 at 17:03

1 Answers1

7

Try this:

$str = chunk_split($string, 30, "\n\r");
// Splits a string after X chars (in this case 30) and adds a line break

You can also try it using regex:

$str = preg_replace("/(.{30})/", "$1\n\r", $string);

Or, as suggested in the comments above, this does the same thing:

$str = wordwrap($string, 30, "<br />\n");

More info:

http://php.net/manual/en/function.chunk-split.php

http://us2.php.net/wordwrap

EDIT:

Based on your edited question, it looks like this is what you're looking for:

$text1    = $this->getUserAgent();
$UAline1  = substr($text1, 0, 26);
$towrapUA = str_replace($UAline1, '', $text1);

$space = str_repeat('&nbsp;', 20);

$wordwrapped = wordwrap($towrapUA, 26, "\n");
$wordwrapped = explode("\n", $wordwrapped); // Split at '\n'

$numlines = count($wordwrapped) - 1;
$string   = '';
$i = 0;

foreach($wordwrapped as $line) {
    if($i < $numlines) {
        $string .= $space . $line . "\n\r"; // Add \n\r back in if not last line
    } else {
        $string .= $space . $line; // If it's the last line, leave off \n\r
    }

    $i++;
}

echo $string;
Chris Clower
  • 5,036
  • 2
  • 18
  • 29
  • I figured out actually very close what I want to do, my only issue at this point is the formatting. Can you take a look at the answer I posted below? – Chris Mirno Aug 04 '12 at 16:56
  • but how do I prefix the return on wordwrap with spaces (below answer)? – Chris Mirno Aug 04 '12 at 17:01
  • @ChrisMirno Updated my answer based on the new info you provided. – Chris Clower Aug 04 '12 at 17:15
  • @ChrisMirno Just remembered you wanted to add spaces before the wrapped lines, updating in a sec... – Chris Clower Aug 04 '12 at 17:20
  • I think I edited over your edit. Let me update it really quick. I was looking at your and its perfect, but the only thing is I need a constant number of spaces before the lines on the wrapped section. In addition I think I want to use chunk_split over wordwrap() because UA's should be split at exactly 26 characters every time. – Chris Mirno Aug 04 '12 at 17:21
  • I can't figure out how to cancel my edit. Moved what I have right now to the bottom of the question. – Chris Mirno Aug 04 '12 at 17:24
  • that looks really good. Let me try it really fast before I check this as accepted. – Chris Mirno Aug 04 '12 at 17:31
  • Works with 1 little error. I got the calls and everything perfect, except for some reason, lets say the last line has 6 UA characters, the last line looks perfect, but for some reason, even though the UA ends on that line, there is now an extra line right after what should be the last line. – Chris Mirno Aug 04 '12 at 17:42
  • Nevermind. My bad. My call ended in a '\n' removed and it works perfectly. thanks for your help! – Chris Mirno Aug 04 '12 at 17:43
  • Your for loop could be replaced with `str_repeat`. Also, you don't need `$text2`, just reuse `$text1`. – Levi Morrison Aug 04 '12 at 17:47
  • Even though it's already accepted, I went ahead and edited it to use Levi's suggestions, and added code to remove the `\n\r` entirely from the last line. Thanks! – Chris Clower Aug 04 '12 at 18:24