How to Pull First 100 Characters of a String in PHP

How do you pull first 100 characters of a string in PHP

$small = substr($big, 0, 100);

For String Manipulation here is a page with a lot of function that might help you in your future work.

How to get first 5 characters from string

For single-byte strings (e.g. US-ASCII, ISO 8859 family, etc.) use substr and for multi-byte strings (e.g. UTF-8, UTF-16, etc.) use mb_substr:

// singlebyte strings
$result = substr($myStr, 0, 5);
// multibyte strings
$result = mb_substr($myStr, 0, 5);

Get first 100 characters from string, respecting full words

All you need to do is use:

$pos=strpos($content, ' ', 200);
substr($content,0,$pos );

Truncate a string to first n characters of a string and add three dots if any characters are removed

//The simple version for 10 Characters from the beginning of the string
$string = substr($string,0,10).'...';

Update:

Based on suggestion for checking length (and also ensuring similar lengths on trimmed and untrimmed strings):

$string = (strlen($string) > 13) ? substr($string,0,10).'...' : $string;

So you will get a string of max 13 characters; either 13 (or less) normal characters or 10 characters followed by '...'

Update 2:

Or as function:

function truncate($string, $length, $dots = "...") {
return (strlen($string) > $length) ? substr($string, 0, $length - strlen($dots)) . $dots : $string;
}

Update 3:

It's been a while since I wrote this answer and I don't actually use this code any more. I prefer this function which prevents breaking the string in the middle of a word using the wordwrap function:

function truncate($string,$length=100,$append="…") {
$string = trim($string);

if(strlen($string) > $length) {
$string = wordwrap($string, $length);
$string = explode("\n", $string, 2);
$string = $string[0] . $append;
}

return $string;
}

Display first 100 characters from the table using echo

you can make use of substr and you can also wrap it using wordwrap

//it would wrap your text upto 20 characters 
$wrappedText = wordwrap(substr($row['description'], 0, 100), 20, "<br />\n", true);

echo '<tbody>
<tr class="server glossed site">
<td class="rank hidden-sm hidden-xs">
'.$rank.'</td>
<td class="description"><p><a href="out.php?id='.$row['id'].'" target="_blank">'.$row['name'].'<br /><p><a href="out.php?id='.$row['id'].'" target="_blank"><img src="'.$row['banner'].'" width="470" height="60"></a></p></a><br><p class="hidden-sm hidden-xs">'.
$wrappedText.'</p></td>
<td class="votes hidden-sm hidden-xs">'.$row['votes'].'</td>
</tr>
';

Print only the first few characters

You can use substr and strrpos.

Substr splits a string and strrpos finds a needle in a haystack backwards.

I use substr to pass 35 characters only to the strrpos and let it find the previous dot.

Then I use that in substr from 0 -> the dot +1 character.

$str = "string with dot. And another. Again.";
// Full string is 36 characters

$n = 35; // in your case 100
$pos = strrpos(substr($str,0,$n), ".");
If($pos === false) $pos = strlen($str);
Echo substr($str,0, $pos+1);

Edit: forgot about if no dot was found.

That makes strrpos return false. If that happens I give $pos the full length of $str.

https://3v4l.org/YS7X7

How to get the first character of string in php

For the single-byte encoded (binary) strings:

substr($username, 0, 1);
// or
$username[0] ?? null;

For the multi-byte encoded strings, such as UTF-8:

mb_substr($username, 0, 1);

How can I get the last 7 characters of a PHP string?

Use substr() with a negative number for the 2nd argument.

$newstring = substr($dynamicstring, -7);

From the php docs:

string substr ( string $string , int $start [, int $length ] )

If start is negative, the returned string will start at the start'th character from the end of string.

Get first and last 100 characters of a text file

try this to get first 100 characters:

$fp = fopen('test.txt', 'r');
$data = fread($fp, 100);
echo $data;
fclose($fp);

and get last 100 characters:

$fp = fopen('test.txt', 'r');
fseek($fp, -100, SEEK_END);
$data = fread($fp, 100);
echo $data;
fclose($fp);


Related Topics



Leave a reply



Submit