{"id":3362,"date":"2017-04-01T16:33:10","date_gmt":"2017-04-01T11:03:10","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3362"},"modified":"2017-04-01T16:33:10","modified_gmt":"2017-04-01T11:03:10","slug":"get-first-n-characters-string","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/get-first-n-characters-string\/","title":{"rendered":"[ Solved -8 Answers] JAVASCRIPT &#8211; Get first n characters of a string"},"content":{"rendered":"<p><label class=\"label label-warning\">PROBLEM:<\/label><\/p>\n<p>How can we get the first n characters of a string in PHP? What&#8217;s the fastest way to trim a string to a specific number of characters, and append &#8216;&#8230;&#8217; if needed?<\/p>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<p>\/\/The simple version for 10 Characters from the beginning of the string.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$string = substr($string,0,10).&#039;...\u2018<\/code><\/pre> <\/div>\n<h4 id=\"update\"><span style=\"color: #ff6600;\"><b>Update:<\/b><\/span><\/h4>\n<ul>\n<li>Based on suggestion for checking length (and also ensuring similar lengths on trimmed and untrimmed strings):<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$string = (strlen($string) &gt; 13) ? substr($string,0,10).&#039;...&#039; : $string;<\/code><\/pre> <\/div>\n<ul>\n<li>So you will get a string of max 13 characters; either 13 (or less) normal characters or 10 characters followed by &#8216;&#8230;&#8217;<\/li>\n<\/ul>\n<h4 id=\"update-2\"><span style=\"color: #993300;\"><b>Update 2:<\/b><\/span><\/h4>\n<p>Or as function:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">function truncate($string, $length, $dots = &quot;...&quot;) <br\/>{<br\/>    return (strlen($string) &gt; $length) ? substr($string, 0, $length - strlen($dots)) . $dots : $string;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>We prefer this function which prevents breaking the string in the middle of a word using the wordwrap function:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">function truncate($string,$length=100,$append=&quot;&hellip;&quot;) <br\/>{<br\/>  $string = trim($string);<br\/><br\/>  if(strlen($string) &gt; $length) <br\/>{<br\/>    $string = wordwrap($string, $length);<br\/>    $string = explode(&quot;\\n&quot;, $string, 2);<br\/>    $string = $string[0] . $append;<br\/>  }<br\/><br\/>  return $string;<br\/>}<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<ul>\n<li>This has been built into PHP since version 4.0.6.<\/li>\n<\/ul>\n<p>from the docs: http:\/\/www.php.net\/manual\/en\/function.mb-strimwidth.php<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">echo mb_strimwidth(&quot;Hello World&quot;, 0, 10, &quot;...&quot;);<br\/><br\/>\/\/ outputs Hello W...<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<ul>\n<li>The Multibyte extension can come in handy, if you need control over the string charset.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$charset = &#039;UTF-8&#039;;<br\/>$length = 10;<br\/>$string = &#039;Hai to yoo! I like yoo soo!&#039;;<br\/>if(mb_strlen($string, $charset) &gt; $length) <br\/>{<br\/>  $string = mb_substr($string, 0, $length - 3, $charset) . &#039;...&#039;;<br\/>}<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<ul>\n<li>Sometimes, you need to limit the string to the last complete word ie: you don&#8217;t want the last word to be broken instead you stop with the second last word.<\/li>\n<li>eg: we need to limit &#8220;This is my String&#8221; to 6 chars but instead of &#8216;This i&#8230;&#8221; we want it to be &#8216;This&#8230;&#8221; ie we will skip that broken letters in the last word.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">class Fun <br\/>{<br\/><br\/>    public function limit_text($text, $len) <br\/>{<br\/>        if (strlen($text) &lt; $len) <br\/>{<br\/>            return $text;<br\/>}<br\/>        $text_words = explode(&#039; &#039;, $text);<br\/>        $out = null;<br\/>  foreach ($text_words as $word) <br\/>{<br\/>            if ((strlen($word) &gt; $len) &amp;&amp; $out == null)<br\/> {<br\/>  return substr($word, 0, $len) . &quot;...&quot;;<br\/> }<br\/>  if ((strlen($out) + strlen($word)) &gt; $len) <br\/>{<br\/>                return $out . &quot;...&quot;;<br\/> }<br\/>            $out.=&quot; &quot; . $word;<br\/> }<br\/>        return $out;<br\/>    }<br\/><br\/>}<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<ul>\n<li>If you want to cut being careful to don&#8217;t split words you can do the following<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">function ellipse($str,$n_chars,$crop_str=&#039; [...]&#039;)<br\/>{<br\/>    $buff=strip_tags($str);<br\/>    if(strlen($buff) &gt; $n_chars)<br\/>    {<br\/>        $cut_index=strpos($buff,&#039; &#039;,$n_chars);<br\/>        $buff=substr($buff,0,($cut_index===false? $n_chars: $cut_index+1)).$crop_str;<br\/>    }<br\/>    return $buff;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>if $str is shorter than $n_chars returns it untouched.<\/li>\n<li>If $str is equal to $n_chars returns it as is as well.<\/li>\n<li>if $str is longer than $n_chars then it looks for the next space to cut or (if no more spaces till the end) $str gets cut rudely instead at $n_chars.<\/li>\n<\/ul>\n<p>NOTE: be aware that this method will remove all tags in case of HTML.<\/p>\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<ul>\n<li>substr() would be best, you&#8217;ll also want to check the length of the string first.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$str = &#039;someLongString&#039;;<br\/>$max = 7;<br\/><br\/>if(strlen($str) &gt; $max) <br\/>{<br\/>   $str = substr($str, 0, $max) . &#039;...&#039;;<br\/>}<\/code><\/pre> <\/div>\n<ul>\n<li>wordwrap won&#8217;t trim the string down, just split it up&#8230;<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<ul>\n<li>Here we use function:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\"> function str_short($string,$limit)<br\/> {<br\/>            $len=strlen($string);<br\/>            if($len&gt;$limit)<br\/>  {<br\/>             $to_sub=$len-$limit;<br\/>             $crop_temp=substr($string,0,-$to_sub);<br\/>             return $crop_len=$crop_temp.&quot;...&quot;;<br\/> }<br\/>            else<br\/> {<br\/>                return $string;<br\/>            }<br\/>        }<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>\u00a0you just call the function with string and limit.<\/li>\n<li>eg: str_short(&#8220;hahahahahah&#8221;,5);<\/li>\n<li>it will cut of your string and add &#8220;&#8230;&#8221; at the end.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 8:<\/label><\/p>\n<h4 id=\"sample-code\"><span style=\"color: #ff6600;\"><b>Sample code:<\/b><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">function cutAfter($string, $len = 30, $append = &#039;...&#039;) <br\/>{<br\/>        return (strlen($string) &gt; $len) ? <br\/>          substr($string, 0, $len - strlen($append)) . $append : <br\/>          $string;<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get first n characters of a string &#8211; The Multibyte extension can come in if you need control over the string charset.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[6254,2134,4089,4153,1128,3066,6255,6253,6256,4088,4087],"class_list":["post-3362","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-getting-the-first-character-of-a-string-with-str0","tag-how-do-i-check-if-a-string-contains-a-specific-word-in-php","tag-how-do-i-get-a-consistent-byte-representation-of-strings-in-c-without-manually-specifying-an-encoding","tag-how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript","tag-how-to-check-whether-a-string-contains-a-substring-in-javascript","tag-how-to-convert-a-string-to-an-int-in-java","tag-how-to-get-the-last-char-of-a-string-in-php","tag-is-there-a-way-to-substring-a-string-in-python","tag-php-string-length","tag-split-a-string-in-c","tag-what-is-the-difference-between-string-and-string-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3362"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3362\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}