Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8362933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:03:28+00:00 2026-06-09T12:03:28+00:00

I am using PHP to return the user’s browser user agent The problem is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:03:29+00:00Added an answer on June 9, 2026 at 12:03 pm

    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

    https://www.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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using PHP and I return this value: $_SESSION['sessionNum'] and I get an
I'm using PHP for a project and I expect -1 % 4 to return
I'm using the following PHP code to return a json string: $itemURL = 'http://***.***.***/search?tag=Football&affiliate_id=&max_results=3';
I'm used to using the command mysql_insert_id() in PHP to return the id of
I am using PHP + jQuery AJAX to log a user into my site.
I am working on an Asset Database problem using PHP / MySQL. In this
I'm building a webapp using php, ajax, javascript, mysql. I've been worring about something
I don't know if it is possible, I am using php with zend-framwork and
What's the best way to print from MySQL using PHP when you know there's
I am getting data from mysql data base using php script problem is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.