Get First 100 Characters from String, Respecting Full Words

Get first 100 characters from string, respecting full words

All you need to do is use:

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

Javascript/jQuery Get first 100 characters from string, respecting full words

The lastIndexOf method takes a second parameter that determines where the search starts, so you don't need to cut the string down before finding the last space:

jQuery(".block-text .success-inner-content").each(function () {
var text = jQuery(this).text();
if (text.length > 100) {
jQuery(this).text(text.substr(0, text.lastIndexOf(' ', 97)) + '...');
}
});

You can also use the text method instead of each to loop the elements and set the text for each:

jQuery(".block-text .success-inner-content").text(function (i, text) {
return text.length > 100 ? text.substr(0, text.lastIndexOf(' ', 97)) + '...' : text;
});

Get N characters from string, respecting full words python

Find the first space after 4000 characters. You can use max to account for text that ends a few characters past 4000, but with no space at the end.

ix = max(data.find(' ', 4000), 4000)
text = data[:ix]

Extract a fixed number of chars from an array, just full words

There are heaps of ways, but try this:

$shortVersion = substr($myvar, 0, 100);
if(strlen($myvar)>100 && preg_match('`\w`', $myvar{100}))
$shortVersion = preg_replace('`\w+$`', '', $shortVersion);

That's probably your path of least resistance.

Shorten string without cutting words in JavaScript

If I understand correctly, you want to shorten a string to a certain length (e.g. shorten "The quick brown fox jumps over the lazy dog" to, say, 6 characters without cutting off any word).

If this is the case, you can try something like the following:

var yourString = "The quick brown fox jumps over the lazy dog"; //replace with your string.
var maxLength = 6 // maximum number of characters to extract

//trim the string to the maximum length
var trimmedString = yourString.substr(0, maxLength);

//re-trim if we are in the middle of a word
trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")))

Make string shorter. Cut it by the last word

This should work:

$str = "i have google too";
$strarr = explode(" ", $str);

$res = "";

foreach($strarr as $k)
{
if (strlen($res.$k)<10)
{
$res .= $k." ";
}
else
{
break;
};
}

echo $res;

http://codepad.org/NP9t4IRi

Making sure PHP substr finishes on a word not a character

It could be done with a regex, something like this will get up to 260 characters from the start of string up to a word boundary:

$line=$body;
if (preg_match('/^.{1,260}\b/s', $body, $match))
{
$line=$match[0];
}

Alternatively, you could maybe use the wordwrap function to break your $body into lines, then just extract the first line.

Split string at space after certain number of characters in Javascript

A short and simple way to split a string into chunks up to a certain length using a regexp:

const chunks = str.match(/.{1,154}(?:\s|$)/g);

some examples:





const str = 'the quick brown fox jumps over the lazy dog';

console.log(str.match(/.{1,10}(?:\s|$)/g))
console.log(str.match(/.{1,15}(?:\s|$)/g))

How to get a get_the_content word limit

Use this handy utility function for restricting word count with respect to words described in here Get first 100 characters from string, respecting full words

function truncate($text, $length) {
$length = abs((int)$length);
if(strlen($text) > $length) {
$text = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1...', $text);
}
return(str_replace(' '," ", $text));
}

Pass the values like

$content = get_the_content('Read more');
print_r(truncate($content, 180));


Related Topics



Leave a reply



Submit