简体   繁体   中英

How to split up a string after a certain number of characters in PHP

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM