. replace with array

0

I'm trying to replace a text that I have in a textarea as something is selected in a select.

The fact is that what I would need is for you to erase the old text to insert the new one, but of course you can not delete all the text just a series of texts that I have stored in mysql. This is so in case the user has put text in that texarea.

The fact is that I have seen that with. replace I can find a text string and replace it with another text, but of course just look for a text string and I have like 10 or 300 possible different chains.

Do you know if there is any way to go through several text strings at the same time and replace them?

Right now what I do is add the new text to the text that already existed using this function:

function PonTexto(id,ndatos,old) {
    var viejosdatos = $('#'+id).val();
    $.ajax({
    url: "Paginas/Textos.php?que=SacarTexto&id="+ndatos,
        type: "post",
        success: function(data) {
            $().toastmessage('showToast', { text : ndatos , sticky : false, type : 'success' });
            $('#'+id).val(viejosdatos + data);
            },
        error: function(xhr, status, error){ $().toastmessage('showToast', { text : 'Error al guardar '+ xhr.responseText, sticky : true, type : 'error' }); }
    });
}

and the function that I am seeing that would replace the text would be something like this:

$('#'+id).html(function(buscayreemplaza, reemplaza) {
        return reemplaza.replace('cadena', 'texto nuevo');
    });

in texts.php there is simply a basic query to mysql that returns a result with the new text. but I understand that I would have to change it for an array.

And textarea is also a basic texarea with no more history.

Can you think of any way I can do this?

Thank you very much for your help.

By touching code here and there I have managed to get to this:

 function PonTexto(id,ndatos) {
    var viejosdatos = $('#'+id).val();
    $.ajax({
    url: "Paginas/Textos?que=SacarTexto&id="+ndatos,
        type: "post",
        success: function(data) {
            $().toastmessage('showToast', { text : data , sticky : false, type : 'success' });
            var viejafp = data.split('-/'); //creo un array con la respuesta separado por un - arr[0] arr[1]
            console.log(viejafp);

            for (var val in viejafp)
            viejosdatos = viejosdatos.replace(new RegExp(val, "gi"), viejafp[val]);

            $('#'+id).val(viejosdatos);
            },
        error: function(xhr, status, error){ $().toastmessage('showToast', { text : 'Error al guardar '+ xhr.responseText, sticky : true, type : 'error' }); }
    });
}

but it does not work I think that because of the format I'm giving it in textos.php that is like this:

foreach($Arrtexto as $Textos) { echo $Textos["Conf12"].":".$Textonuevo["Conf12"]."-/"; }

How could you format that text so that it has a format like this?

{"-":"X", "_":"Y", "\+":"Z"};
    
asked by Killpe 14.09.2017 в 19:57
source

1 answer

1

The main task would be to place the new texts in a variable, consider that it is an original pair: replacement but the most used is now JSON.

Your Texts.php file should return that JSON object, for them you can use json_encode (miArrayData) ;

var texto = "Hola Pago al contado";
console.log(texto);
//este data seria el json retornado por Textos.php
var data=[{original: "Forma de pago: 20% del total a la confirmación del pedido y el resto antes de la entrega.", reemplazo: "Pago con PayPal a la cuenta XXX"}, {original: "Pago con PayPal a la cuenta XXX", reemplazo: "Pago con PayPal a la cuenta XXX"}, {original: "Cobro en cuenta de cliente", reemplazo: "Pago con PayPal a la cuenta XXX"}, {original: "Pago al contado", reemplazo: "Pago con PayPal a la cuenta XXX"}, {original: "Papelujo para mi", reemplazo: "Pago con PayPal a la cuenta XXX"}];

var nuevoTexto=replaceAll(texto,data);
console.log(nuevoTexto);

//////////////////////////////////
function replaceAll(texto,data){
    for(i=0;i<data.length;i++){
            if(texto.indexOf(data[i].original)<0 && texto.indexOf(data[i].reemplazo)<0){//agregando los no existentes
                texto=texto + data[i].reemplazo;
            }else{//reemplazando los existentes
                if(texto.indexOf(data[i].reemplazo)<0){
                    texto=texto.replace(new RegExp(data[i].original, 'g'),data[i].reemplazo);
                }else{
                    texto=texto.replace(new RegExp(data[i].original, 'g'),"");
                }
            }
    }
    return texto;
}

PHP, array a json

Build your database response something like this:

$data= array();
//usar un for para todos tus registros
array_push($data,array("original"=>$row['valorO'],"reemplazo"=>$row['valorR']));
array_push($data,array("original"=>$row['valorO'],"reemplazo"=>$row['valorR']));
array_push($data,array("original"=>$row['valorO'],"reemplazo"=>$row['valorR']));

//responder como json
echo json_encode($data);
    
answered by 14.09.2017 / 21:46
source