{"id":4,"date":"2013-10-19T01:02:30","date_gmt":"2013-10-19T01:02:36","guid":{"rendered":"https:\/\/ca219f914elogo.jpg"},"modified":"2015-07-16T18:00:13","modified_gmt":"2015-07-17T00:00:13","slug":"cortar-caracteres-sin-cortar-las-palabras","status":"publish","type":"post","link":"https:\/\/emanuelpaxtian.com\/blog\/cortar-caracteres-sin-cortar-las-palabras\/","title":{"rendered":"Cortar caracteres sin cortar las palabras"},"content":{"rendered":"<p>En esta ocasi\u00f3n se muestra la forma de como cortar un texto con php sin cortar las palabras se puede definir la cantidad de caracteres por default tiene 180 y si se pasa en autom\u00e1tico pone tres puntos suspensivos.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\"> cortar($texto, $longitud = 180) { \r\nif((mb_strlen($texto) &amp;gt; $longitud)) { \r\n    $pos_espacios = mb_strpos($texto, ' ', $longitud) - 1; \r\n    if($pos_espacios &amp;gt; 0) { \r\n        $caracteres = count_chars(mb_substr($texto, 0, ($pos_espacios + 1)), 1); \r\n        if ($caracteres&#x5B;ord('&amp;lt;')] &amp;gt; $caracteres&#x5B;ord('&amp;gt;')]) { \r\n            $pos_espacios = mb_strpos($texto, &quot;&amp;gt;&quot;, $pos_espacios) - 1; \r\n        } \r\n        $texto = mb_substr($texto, 0, ($pos_espacios + 1)).'...'; \r\n    } \r\n    if(preg_match_all(&quot;|(&amp;lt;(&#x5B;\\w]+)&#x5B;^&amp;gt;]*&amp;gt;)|&quot;, $texto, $buffer)) { \r\n        if(!empty($buffer&#x5B;1])) { \r\n            preg_match_all(&quot;||&quot;, $texto, $buffer2); \r\n            if(count($buffer&#x5B;2]) != count($buffer2&#x5B;1])) { \r\n                $cierrotags = array_diff($buffer&#x5B;2], $buffer2&#x5B;1]); \r\n                $cierrotags = array_reverse($cierrotags); \r\n                foreach($cierrotags as $tag) { \r\n                        $texto .= ''; \r\n                } \r\n            } \r\n        } \r\n    } \r\n \r\n} \r\nreturn $texto; \r\n} \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En esta ocasi\u00f3n se muestra la forma de como cortar un texto con php sin cortar las palabras se puede definir la cantidad de caracteres por default tiene 180 y&hellip;<\/p>\n","protected":false},"author":1,"featured_media":411,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-4","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":0,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/media\/411"}],"wp:attachment":[{"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}