Delete blank space inside a tag p

0

I have the following problem, I am uploading a paragraph to a view in html . The problem I have, is that I'm placing blank space within the p tag of html . Which when reading the content of that paragraph, with javascript to replace the text does not realize it since it finds me this . I leave the code to see if you can help me.

  

Note: I am inserting the text into the database through   ckeditor I need it to be from php , removing the spaces within   the label. Since if I do it by javascript it does not replace the text   complete but a part.

Function php.

public function getParrafo(){
        $tipo = input_post('tipo');
        $obj = $this->pre->get_preguntaId($this->id);
        if($tipo == 27 OR $tipo == 33){
            $texto = strip_tags(trim($obj->contenido),"<p>");
        }else{
            $texto = strip_tags($obj->contenido,chr(13).chr(10));
        }
        $jdata['texto'] = $texto;
        echo json_encode($jdata);
  

This is what I get from the php in the echo.

<p>
    La Convención de las Naciones Unidas sobre el Derecho del Mar, el tratado más importante jamás negociado en ese ámbito, es relativamente reciente. Entró en vigor el 16 de noviembre de 1994. Pero sus orígenes se remontan a comienzos de los años cincuenta, época en que se creó la Comisión de Derecho Internacional de las Naciones Unidas, encargada de desarrollar teniendo en cuenta la práctica de los Estados, el derecho internacional, gran parte del cual era necesario redactar.</p>

jquery

    $.post('index.php?c=unidades&f=getParrafo',{id:id,tipo:tipo},function(data){
        $("div#texto0").html(data.texto);
    },'json');

html

<p>
            La Convención de las Naciones Unidas sobre el Derecho del Mar, el tratado más importante jamás negociado en ese ámbito, es relativamente reciente. Entró en vigor el 16 de noviembre de 1994. Pero sus orígenes se remontan a comienzos de los años cincuenta, época en que se creó la Comisión de Derecho Internacional de las Naciones Unidas, encargada de desarrollar teniendo en cuenta la práctica de los Estados, el derecho internacional, gran parte del cual era necesario redactar.</p>  
  

This is how I read it javascript

 "

    &nbsp;

    La Convención de las Naciones"
    
asked by Yoel Rodriguez 26.04.2017 в 19:23
source

3 answers

1

Use trim ()

public function getParrafo(){
        $tipo = input_post('tipo');
        $obj = $this->pre->get_preguntaId($this->id);
        if($tipo == 27 OR $tipo == 33){
            $texto = strip_tags($obj->contenido);
            // borra todo tipo de espacios, al inicio y al final, y concateno las etiquetas removidas
            $texto = "<p>".trim($texto)."</p>";
        }else{
            $texto = strip_tags($obj->contenido,chr(13).chr(10));
        }
        $jdata['texto'] = $texto;
        echo json_encode($jdata);
    
answered by 26.04.2017 в 19:38
0

You can clean it on the client side or on the server side

on the client side with javascript

data.texto.trim()

on the server side with PHP

$jdata['texto'] = trim($texto , " ");
    
answered by 26.04.2017 в 19:45
0

Here is the solution I found, I hope it will be useful to someone else. What I did was create a function in which I remove the spaces:

  

Function to remove spaces

    public function getextoarray($texto){
    //Pasmos de string a array por la etiqueta </p>
    $a_texto = explode("</p>", $texto);
    foreach($a_texto as $val){
        $r_texto = '';
        if($val != ""){
            $r_texto = trim(str_replace("<p>","",$val));
            $s_texto .= "<p>".$r_texto."</p>";
        }
    }
    return $s_texto;
}
  

Integration in the original appointment

    public function getParrafo(){
    $tipo = input_post('tipo');
    $obj = $this->pre->get_preguntaId($this->id);
    if($tipo == 27 OR $tipo == 33){
        $texto = $this->getextoarray(strip_tags(trim($obj->contenido, " "),"<p>"));
    }else{
        $texto = strip_tags($obj->contenido,chr(13).chr(10));
    }
    $jdata['texto'] = $texto;
    echo json_encode($jdata);
}
    
answered by 27.04.2017 в 01:25