cj333
cj333

Reputation: 2609

php add <br /> only after first 100 characters

How to add <br /> only after first 100 characters? I mean just break once, not add <br /> after each 100 characters.

$str="Apple has announced that co-founder, former CEO and chairman of the board Steve Jobs has passed away at the age of 56, so we take a look back at the famous innovator's life.At left, Steve Jobs, chairman of the board of Apple Computer, leans on the new 'Macintosh' personal computer following a shareholder's meeting Jan. 24, 1984, in Cupertino, Calif. The Macintosh was priced at $2,495.";
//echo substr($str, 0, 100).'<br />'; this will break sentence after 100 characters. 
//echo wordwrap($str, 100, "\n<br />\n");this will add <br /> after each 100 characters.

And what I need is:

Apple has announced that co-founder, former CEO and chairman of the board Steve Jobs has passed away    
<br /> <-- only add here.
at the age of 56, so we take a look back at the famous innovator's life.At left, Steve Jobs, chairman of the board of Apple Computer, leans on the new 'Macintosh' personal computer following a shareholder's meeting Jan. 24, 1984, in Cupertino, Calif. The Macintosh was priced at $2,495.

Upvotes: 1

Views: 3312

Answers (6)

Tobias
Tobias

Reputation: 9380

You can use substr to get the first 100 characters and add a <br /> then and get all other chars after. Example:

<?php
    echo substr( $string , 0, 100)
         , "<br />"
         , substr( $string, 100 );
?> 

Upvotes: 3

mario
mario

Reputation: 145482

If you meant to write "break up after 100 letters" instead of characters, then substr won't do. And wordwrap does work recurringly. So you would need to manually find 100 letters and inject a break there:

$str = preg_replace('/^.{100,}?\b/s', "$0<br>", $str);

Upvotes: 6

hakre
hakre

Reputation: 197832

PHP has a function to insert a string into another at a specific position, it's called substr_replace:

echo substr_replace($str, '<br>', 100, 0);

If you need to support UTF-8 (you have not specified any encoding), you can do this with a regular expression and preg_replace which allows to not cut words, too:

echo preg_replace('/^.{80,120}+\b/su', '$0<br>', $str);

Upvotes: 5

Nexerus
Nexerus

Reputation: 1088

This could work:

$text = "...";
if (isset($text[100])) {
    $text[100] = $text[100] . "<br />";
}

Upvotes: 0

shuniar
shuniar

Reputation: 2622

I would do something like this

<?
    $str="Apple has announced that co-founder, former CEO and chairman of the board Steve Jobs has passed away at the age of 56, so we take a look back at the famous innovator's life.At left, Steve Jobs, chairman of the board of Apple Computer, leans on the new 'Macintosh' personal computer following a shareholder's meeting Jan. 24, 1984, in Cupertino, Calif. The Macintosh was priced at $2,495.";

    for ($i = 0; $i < strlen($str); $i += 99)
    {
        if ((strlen($str) - $i) < 100) {
            echo substr($str, $i, strlen($str));
        } else {
            echo substr($str, $i, 100);
        }

        echo "<br />";
    }
?>

Upvotes: 0

santiagobasulto
santiagobasulto

Reputation: 11686

$str="Apple has announced that co-founder, former CEO and chairman of the board Steve Jobs has passed away at the age of 56, so we take a look back at the famous innovator's life.At left, Steve Jobs, chairman of the board of Apple Computer, leans on the new 'Macintosh' personal computer following a shareholder's meeting Jan. 24, 1984, in Cupertino, Calif. The Macintosh was priced at $2,495.";
$first_part = substr ( $str ,0,100);
$second_part = substr ( $str ,100); //Goes to end of str
$new = $first_part."<br />".$second_part;

Upvotes: 1

Related Questions