I am using one function
to shorten words, but what I really want is to create in the function
shorten string, without shortening words
function getSubString($string, $length=NULL){
//Si no se especifica la longitud por defecto es 50
if ($length == NULL)
$length = 15;
//Primero eliminamos las etiquetas html y luego cortamos el string
$stringDisplay = substr(strip_tags($string), 0, $length);
//Si el texto es mayor que la longitud se agrega puntos suspensivos
if (strlen(strip_tags($string)) > $length)
$stringDisplay .= '...';
return $stringDisplay;
}
//Veamos la ejecución de la misma.
$texto = "Este es un acortador de palabras, y yo deseo ser un acortador de cadenas no de palabras";
echo '<span>'.getSubString($product).'</span>';
The result that is printed is the following:
This is a hair shortener...
When inspecting the source code with the browser's developer tool it shows the following:
<span>Este es un acortador de palabras, y yo deseo ser un acortador de cadenas no de palabras</span>
Update
After having had so many very interesting answers but all unfortunately failed.
I came up with the idea using jQuery to delimit the word and then join it, for example we are going to limit the characters to be displayed from 10
any text, taking as an example the following text *"php 7 basic" the result will be the following:
php 7...
Inspecting the output with the browser's developer tool will display the following: (not what I thought) but more ideal
<h2 id="result">php 7...<h2 id="original" style="display: none;"> basic</h2></h2>
Why ideal?
Because this way it will not show an incomplete text in the future in the search engines, now if we remove the display:none;
it will show this:
php 7...basic
And it's perfect but there are two problems:
It is adding
id
to the tagh2
and since there are several elements to trim, this will not be valid for the correct validation of HTML, the HTML validation documentation says that there should not be more than one content with theid
same name, the solution is to migrate the a jQuery codeid
inclass
an article toclass
use.each()
its jQuery functions.The second error is that it only works with a single element that contains the
id="original"
if I add more than one in the same document it only shortens the first one the rest of the elements don't.
$(function() {
var maxlength = 10;
var dots = $('#original').text().length>maxlength?'...':'';
var hello = $('#original').text().length>maxlength?$('#original').text().substring(0,maxlength).replace(/\W\w+\s*(\W*)$/, '$1'):$('#original').text();
$('#original').before('<h2 id="result">'+hello+dots+'</h2>')
$('#original').text($('#original').text().substring(hello.length))
.css('display','none')
.appendTo('#result');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 id="original">php 7 basic</h2>
<h2 id="original">Otro texto de otro contenedor</h2>
<h2 id="original">Otro texto de otro contenedor dos</h2>
In the javascript(jQuery) section I changed this
('#original')
to this$('.original')
but it didn't work, when making those changes the first content is repeated and the rest is ignored, it can be seen in the following link https://jsfiddle.net/mrfndefx/