Codificare parti ti un testo

Veronica Auretta

Nuovo Utente
21 Apr 2015
25
0
0
Criptare parti di un testo

Salve, avrei bisogno di una mano con questo procedimento.

Parto da un testo che prendo dal db e devo stamparlo a video. Nel testo alcune parti devono essere criptate e le altre rimanere normali.

per la criptazione ho scritto questa funzione (che mi permette di criptare mantenendo il numero dei caratteri e gli spazi tra le parole)

Codice:
<?php function LettereCasuali($lunghezza){
	$caratteri ="abcdefghijklmnopqrstuvwxyz";
	$code = "";
	for($i = 0; $i<$lunghezza; $i++){
		$code = $code.substr($caratteri,rand(0,strlen($caratteri)-1),1);
	}
	return $code;
}

function CodificaLingua($testo){
    
$parole = explode(' ',$testo);
$codificato = '';
foreach($parole as $parola) {    
  
$quante = strlen($parola);
$codificato.=LettereCasuali($quante).' ';
}
 return trim($codificato);  
}

e fin qui tutto bene... ora la domanda è: come sostituisco solo determinate parole all'interno di una frase?
Ho provato ad usare preg_replace in vari modi e senza molti risultati (anche perché non sono pratica)

Codice:
function traduci($str)
{
    $search = array(
        '#\&lt;&sect;I(.+?)\&gt;#is' 
    );
    $replace = array(
	    '&lt;'.CodificaLingua(\'$1\').'&gt;' //QUI VORREI LA FRASE CRIPTATA
    );
    return preg_replace($search, $replace, $str); 
}


quindi dato un testo del tipo: <§I testo da criptare> testo da non criptare.
dovrebbe stamparmi questo: <kijth js rntdkrb> testo da non criptare.

ma ovviamente non funziona XD mi stampa solo due caratteri a caso evidentemente non riuscendo a leggere quel $1 all'interno del replace, come si può fare...?
 
Ultima modifica:

marino51

Utente Attivo
28 Feb 2013
3.203
207
63
Lombardia
PHP:
	$replace = array( '&lt;'.CodificaLingua(\'$1\').'&gt;' ); //QUI VORREI LA FRASE CRIPTATA

PHP Parse error: syntax error, unexpected ''$1\').'' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING)
 

Veronica Auretta

Nuovo Utente
21 Apr 2015
25
0
0
PHP:
	$replace = array( '&lt;'.CodificaLingua(\'$1\').'&gt;' ); //QUI VORREI LA FRASE CRIPTATA

Sì... a parte QUESTA sintassi sbagliata che, sul mio codice ho scritto giusta e ho sbagliato a riportare qui... non è comunque la soluzione, perché lo stesso non funziona correggendolo. Quello che chiedo è proprio come posso fare per eseguire una funzione all'interno del replace o (molto probabile) se ci sia un modo del tuto diverso di fare la stessa cosa :D

PHP:
	$replace = array( '&lt;'.CodificaLingua('$1').'&gt;' ); //QUI VORREI LA FRASE CRIPTATA

Ecco qua... in questo modo mi legge $1 come se fossero due normalissimi caratteri e mi stampa un codice casuale di lunghezza due caratteri.
 
Ultima modifica:

marino51

Utente Attivo
28 Feb 2013
3.203
207
63
Lombardia
forse non ho capito nulla del tuo codice, perché ci sono alcune incongruenze (non vedendo l'intero processo)
messo così a me funziona
ciao
Marino
PHP:
<?php

$testo  = 'The string or an array with strings to replace. <§I testo da criptare>If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. <§I testo da criptare>If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string. 
replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n"th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\\\" PHP string). 
When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \\1 notation for your backreference. \\11, for example, would confuse preg_replace() since it does not know whether you want the \\1 backreference followed by a literal 1, or the \\11 backreference followed by nothing. In this case the solution is to use \${1}1. <§I testo da criptare>This creates an isolated $1 backreference, leaving the 1 as a literal. 
';

var_dump($testo);
echo "<br /> <br />";
var_dump(traduci($testo));


function LettereCasuali($lunghezza){
	$caratteri ="abcdefghijklmnopqrstuvwxyz";
	$code = "";
	for($i = 0; $i<$lunghezza; $i++){
		$code = $code.substr($caratteri,rand(0,strlen($caratteri)-1),1);
	}
	return $code;
}

function CodificaLingua($testo){
	$parole = explode(' ',$testo);
	$codificato = '';
	foreach($parole as $parola) {    
		$quante = strlen($parola);
		$codificato.=LettereCasuali($quante).' ';
	}
	return trim($codificato);  
}

function traduci($str)
{
//	$search  = array( '#\&lt;&sect;I(.+?)\&gt;#is' );
//	$replace = array( '&lt;'.CodificaLingua(\'$1\').'&gt;' ); //QUI VORREI LA FRASE CRIPTATA

	$search  = array( '<§I testo da criptare>' );
	var_dump($search);
	echo "<br /> <br />";
	$replace = array( '&lt;'.CodificaLingua('<§I testo da criptare>').'&gt;' );
	var_dump($replace);
	echo "<br /> <br />";
	return preg_replace($search, $replace, $str); 
}
?>
 

Veronica Auretta

Nuovo Utente
21 Apr 2015
25
0
0
forse non ho capito nulla del tuo codice, perché ci sono alcune incongruenze (non vedendo l'intero processo)
messo così a me funziona
ciao
Marino
PHP:
<?php

$testo  = 'The string or an array with strings to replace. <§I testo da criptare>If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. <§I testo da criptare>If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string. 
replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n"th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\\\" PHP string). 
When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \\1 notation for your backreference. \\11, for example, would confuse preg_replace() since it does not know whether you want the \\1 backreference followed by a literal 1, or the \\11 backreference followed by nothing. In this case the solution is to use \${1}1. <§I testo da criptare>This creates an isolated $1 backreference, leaving the 1 as a literal. 
';

var_dump($testo);
echo "<br /> <br />";
var_dump(traduci($testo));


function LettereCasuali($lunghezza){
	$caratteri ="abcdefghijklmnopqrstuvwxyz";
	$code = "";
	for($i = 0; $i<$lunghezza; $i++){
		$code = $code.substr($caratteri,rand(0,strlen($caratteri)-1),1);
	}
	return $code;
}

function CodificaLingua($testo){
	$parole = explode(' ',$testo);
	$codificato = '';
	foreach($parole as $parola) {    
		$quante = strlen($parola);
		$codificato.=LettereCasuali($quante).' ';
	}
	return trim($codificato);  
}

function traduci($str)
{
//	$search  = array( '#\<§I(.+?)\>#is' );
//	$replace = array( '<'.CodificaLingua(\'$1\').'>' ); //QUI VORREI LA FRASE CRIPTATA

	$search  = array( '<§I testo da criptare>' );
	var_dump($search);
	echo "<br /> <br />";
	$replace = array( '<'.CodificaLingua('<§I testo da criptare>').'>' );
	var_dump($replace);
	echo "<br /> <br />";
	return preg_replace($search, $replace, $str); 
}
?>

Ecco il problema è proprio qui

Codice:
$search  = array( '<§I testo da criptare>' );
$replace = array( '&lt;'.CodificaLingua('<§I testo da criptare>').'&gt;' );

Io non devo cercare nel testo "<§I testo da criptare>" era un esempio quello, mi sarò spiegata male. Io devo cercare un testo compreso tra "<§I" e ">" all'interno di un testo dove questa formula può comparire anche più di una volta. Per questo ho fatto...

Codice:
$search = array('#\&lt;&sect;I(.+?)\&gt;#is');

...altrimenti che senso aveva complicarmi la vita? XD
trovarlo lo trova... ripeto, non riesco a fare il raplace con all'interno una funzione che non sia

Codice:
CodificaLingua('<§I testo da criptare>')

così funziona anche a me... e stampa <§I testo da criptare> criptato... ma non è così che deve funzionare, deve richiamare quella parte di testo compresa tra i due delimitatori... cioè quella che nelle funzioni è identificata con $1
 

marino51

Utente Attivo
28 Feb 2013
3.203
207
63
Lombardia
riparti da qui ...
a me funziona, devi vedere però come gestire tutti i testi trovati ($1 per intenderci)

puoi usare $matches[0], mentre $matches[1] contiene il testo pulito dai delimitatori

ciao
Marino
PHP:
<?php

// Io devo cercare un testo compreso tra "<§I" e ">" all'interno di un testo 
// dove questa formula può comparire anche più di una volta.

$testo  = 'The string or an array with strings to replace. <§I testo da criptare aa>If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. <§I testo da criptare bb>If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string. 
replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n"th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\\\" PHP string). 
When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \\1 notation for your backreference. \\11, for example, would confuse preg_replace() since it does not know whether you want the \\1 backreference followed by a literal 1, or the \\11 backreference followed by nothing. In this case the solution is to use \${1}1. <§I testo da criptare cc>This creates an isolated $1 backreference, leaving the 1 as a literal. 
';

var_dump($testo);
echo "<br /> <br />";
var_dump(traduci($testo));


function LettereCasuali($lunghezza){
	$caratteri ="abcdefghijklmnopqrstuvwxyz";
	$code = "";
	for($i = 0; $i<$lunghezza; $i++){
		$code = $code.substr($caratteri,rand(0,strlen($caratteri)-1),1);
	}
	return $code;
}

function CodificaLingua($testo){
	$parole = explode(' ',$testo);
	var_dump($parole);
	echo "<br /> <br />";
	$codificato = '';
	foreach($parole as $parola) {
  		$quante = strlen($parola);
		$codificato.=LettereCasuali($quante).' ';
	}
	return trim($codificato);  
}

function traduci($str)
{
//	$search  = array( '#\&lt;&sect;I(.+?)\&gt;#is' );
//	var_dump($search);
//	echo "<br /> <br />";

	preg_match_all('/<§I(.*?)>/s', $str, $matches);
	var_dump($matches);
	echo "<br /> <br />";


	$replace = array( '&lt;'.CodificaLingua('$1').'&gt;' );
	var_dump($replace);
	echo "<br /> <br />";

	return preg_replace($matches[0], $replace, $str); 
}
?>
 

Veronica Auretta

Nuovo Utente
21 Apr 2015
25
0
0
Scusa, non ho capito, come fai a dire che ti funziona? a me da questo risultato:

string(79) "The string or an array with strings to replace. <&lt;ys&gt;>"

che ripulito da tutto sarebbe "ys" ha criptato solo due caratteri (cioè "$1"), non l'intero testo nei delimitatori.

avevo provato una cosa simile io facendo così

Codice:
preg_match_all("(\&lt;I&sect;(.+?)\&gt;)", $testo , $risultato);
$array = $risultato;
$n = count($array);
for ($i=0; $i<=$n; $i++)
{$trad= "<".CodificaLingua($array[1][$i]).">";}

il risultato è che riesco a stampare il testo codificato tra i delimitatori in questo modo <gasda sadjajs dada><adajda dsj sdajda>
ma tutto il testo che sta tra l'uno e l'altro me lo perdo.
Per esempio...

Se il testo è: <§I testo da criptare> testo da non criptare <§I testo da criptare>
Il risultato, con il ciclo for è: <htrjk je kirntamr><perslt js ncridmalt>
...e non so come recupareare "testo da non criptare"
 

marino51

Utente Attivo
28 Feb 2013
3.203
207
63
Lombardia
pensavo riuscissi a vedere come funzionava e quindi a sistemare le cose per la tua necessità,

proviamo a leggere insieme cosa è presente nello script,

nel testo ho inserito,
"<§I testo da criptare aa>", "<§I testo da criptare bb>" e "<§I testo da criptare cc>"
(ma protrebbe anche esserci una stringa ripetuta, non differenziata)

l'istruzione,
PHP:
	preg_match_all('/<§I(.*?)>/s', $str, $matches);
	var_dump($matches);
	echo "<br /> <br />";

restituisce correttamente le stringhe trovate con i delimitatori che vuoi tu,
array(2) { [0]=> array(3) { [0]=> string(25) "<§I testo da criptare aa>" [1]=> string(25) "<§I testo da criptare bb>" [2]=> string(25) "<§I testo da criptare cc>" } [1]=> array(3) { [0]=> string(21) " testo da criptare aa" [1]=> string(21) " testo da criptare bb" [2]=> string(21) " testo da criptare cc" } }
in particolare
l'elemento "[0]" dell'array contiene le stringhe trovate,
l'elemento "[1]" dell'array contiene le stringhe trovate, ripulite dei delimitatori

a questo punto tu dovresti scorrere l'array $matches[1] e per quanti elementi presenti, eseguire
PHP:
$replace[0....n]=CodificaLingua($matches[1][0....n])
trovando così la stringa che sostituirà gli elementi delimitati

a questo punto il comando,
PHP:
return preg_replace($matches[0], $replace, $str);
potrà funzionare correttamente (come dimostrato anche dalla prova, seppur mancando la giusta cifratura, rappresentata però dai 2 caratteri "$1")

per comodità ti posto anche il log completo dove puoi vedere che il tutto arriva al risultato, salvo i passi che speravo completassi tu
string(1519) "The string or an array with strings to replace. <§I testo da criptare aa>If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. <§I testo da criptare bb>If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string. replacement may contain references of the form \n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n"th parenthesized pattern. n can be from 0 to 99, and \0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\" PHP string). When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \1 notation for your backreference. \11, for example, would confuse preg_replace() since it does not know whether you want the \1 backreference followed by a literal 1, or the \11 backreference followed by nothing. In this case the solution is to use \${1}1. <§I testo da criptare cc>This creates an isolated $1 backreference, leaving the 1 as a literal. "

array(2) { [0]=> array(3) { [0]=> string(25) "<§I testo da criptare aa>" [1]=> string(25) "<§I testo da criptare bb>" [2]=> string(25) "<§I testo da criptare cc>" } [1]=> array(3) { [0]=> string(21) " testo da criptare aa" [1]=> string(21) " testo da criptare bb" [2]=> string(21) " testo da criptare cc" } }

array(1) { [0]=> string(2) "$1" }

array(1) { [0]=> string(10) "<cj>" }

string(1460) "The string or an array with strings to replace. <<cj>>If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. <>If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string. replacement may contain references of the form \n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n"th parenthesized pattern. n can be from 0 to 99, and \0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\" PHP string). When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \1 notation for your backreference. \11, for example, would confuse preg_replace() since it does not know whether you want the \1 backreference followed by a literal 1, or the \11 backreference followed by nothing. In this case the solution is to use \${1}1. <>This creates an isolated $1 backreference, leaving the 1 as a literal. "

se ti serve altro fai sapere
ciao
Marino
 

Veronica Auretta

Nuovo Utente
21 Apr 2015
25
0
0
ma i passi che tu speravi completassi io sono proprio quelli che non riesco a fare... tutto il resto mi è chiaro XD

riesco anche io ad arrivare a quell'array e scegliermi i casi che mi occorrono, anzi, vado anche oltre riuscendo anche a decodificarli correttamente, quello che non riesco a fare, usando il preg_match_all (rileggi il mio post precedente) è avere il resto del testo, quello che non è tra i delimitatori. Spero sia più chiaro ora... :( non so come altro spiegarmi
 

marino51

Utente Attivo
28 Feb 2013
3.203
207
63
Lombardia
PHP:
function traduci($str)
{
//	$search  = array( '#\&lt;&sect;I(.+?)\&gt;#is' );

	preg_match_all('/<§I(.*?)>/s', $str, $matches);
	var_dump($matches[0]);
	echo "<br /> <br />";
	var_dump($matches[1]);
	echo "<br /> <br />";

	$Nstringhe = count($matches[1]);
	var_dump($Nstringhe);
	echo "<br /> <br />";

	$replace = array();
	for ($i=0; $i<$Nstringhe; $i++)
		$replace[$i] = '&lt;'.CodificaLingua( $matches[1][$i] ).'&gt;'; // non so se devi lasciare i tag nel testo finito
	var_dump($replace);
	echo "<br /> <br />";

	return preg_replace($matches[0], $replace, $str); 
}

string(1519) "The string or an array with strings to replace. <§I testo da criptare aa>If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. <§I testo da criptare bb>If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string. replacement may contain references of the form \n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n"th parenthesized pattern. n can be from 0 to 99, and \0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\" PHP string). When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \1 notation for your backreference. \11, for example, would confuse preg_replace() since it does not know whether you want the \1 backreference followed by a literal 1, or the \11 backreference followed by nothing. In this case the solution is to use \${1}1. <§I testo da criptare cc>This creates an isolated $1 backreference, leaving the 1 as a literal. "

array(3) { [0]=> string(25) "<§I testo da criptare aa>" [1]=> string(25) "<§I testo da criptare bb>" [2]=> string(25) "<§I testo da criptare cc>" }

array(3) { [0]=> string(21) " testo da criptare aa" [1]=> string(21) " testo da criptare bb" [2]=> string(21) " testo da criptare cc" }

int(3)

array(3) { [0]=> string(28) "<dzkit qi xijjnnei gq>" [1]=> string(28) "<zdnwm xg fwhxbeko rm>" [2]=> string(28) "<flgng cl zfopvulh ri>" }

string(1534) "The string or an array with strings to replace. <<dzkit qi xijjnnei gq>>If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. <<zdnwm xg fwhxbeko rm>>If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string. replacement may contain references of the form \n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n"th parenthesized pattern. n can be from 0 to 99, and \0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\" PHP string). When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \1 notation for your backreference. \11, for example, would confuse preg_replace() since it does not know whether you want the \1 backreference followed by a literal 1, or the \11 backreference followed by nothing. In this case the solution is to use \${1}1. <<flgng cl zfopvulh ri>>This creates an isolated $1 backreference, leaving the 1 as a literal. "
 

Veronica Auretta

Nuovo Utente
21 Apr 2015
25
0
0
Non so perché mi è scomparsa la risposta che avevo dato... <.<

la questione è la seguente, io ho già fatto questo array con preg_match_all (se rileggi il post prima di questo si vede), estraggo correttamente i dati con il ciclo for e li cripto. Il problema non è questo, è che non riesco poi a "ricostruire" il testo completo... vabbhè ora ci ragiono un po' su comunque.

Ok niente... non mi aggiornava la risposta, ora leggo sopra, scusa -.-

Grazie di tutto, così funziona :D
 
Ultima modifica:

Veronica Auretta

Nuovo Utente
21 Apr 2015
25
0
0
per me è "ermetico", non capisco
ciao
Marino

No, niente XD non avevo visto la risposta, non mi aveva aggiornato... ho risolto, in parte, mi rimane un ultimo dubbio.

Come mai se sostituisco questo

Codice:
preg_match_all('/<§I(.*?)>/s', $str, $matches);
con questo
Codice:
preg_match_all('/&lt;I&sect;(.*?)&gt;/s', $str , $matches);

mi da il seguente errore? (ovviamente cambio la sintassi anche nel testo di prova)

<b>Warning</b>: preg_replace(): Unknown modifier 'c' in <b>[...][...]</b> on line <b>51</b><br />
NULL

per me è essenziale poter cercare i delimitatori scritti in queto modo. :(
 

marino51

Utente Attivo
28 Feb 2013
3.203
207
63
Lombardia
PHP:
$regex = "'#\&lt;&sect;I(.+?)\&gt;#is'";
var_dump($regex); echo "<br /> <br />";
$regex = "#\<§I(.+?)\>#is";
var_dump($regex); echo "<br /> <br />";

string(28) "'#\<§I(.+?)\>#is'"

string(15) "#\<§I(.+?)\>#is"

il problema sta qui probabilmente,
dichiarando i caratteri speciali nella stringa, il browser li converte per visualizzarli ma nella stringa ci sono loro
la stringa é lunga 28 !!

usando i caratteri "leggibili" la stringa è lunga 15 come deve

risultato, la prima non funziona, la seconda si

se vuoi proprio usare la prima, bisogna trovare una conversione "tipo browser" da applicare alla stringa
 
Ultima modifica:

marino51

Utente Attivo
28 Feb 2013
3.203
207
63
Lombardia
che dici di provare così .... ma per favore salva lo script in UTF-8 ( !!!! )

PHP:
function traduci($str)
{
	$regex = '#\&lt;&sect;I(.+?)\&gt;#is';
	var_dump($regex); echo "<br /> <br />";

	$regex = html_entity_decode($regex);
	var_dump($regex); echo " --- html_entity_decode<br /> <br />";

	preg_match_all($regex, $str, $matches);
	var_dump($matches[0]); echo "<br /> <br />";
	var_dump($matches[1]); echo "<br /> <br />";

	$Nstringhe = count($matches[1]);
	var_dump($Nstringhe); echo "<br /> <br />";

	$replace = array();
	for ($i=0; $i<$Nstringhe; $i++)
		$replace[$i] = '&lt;'.CodificaLingua( $matches[1][$i] ).'&gt;';
	var_dump($replace); echo "<br /> <br />";

	return str_replace($matches[0], $replace, $str); 
}
 
Ultima modifica:

Veronica Auretta

Nuovo Utente
21 Apr 2015
25
0
0
che dici di provare così .... ma per favore salva lo script in UTF-8 ( !!!! )

PHP:
function traduci($str)
{
	$regex = '#\&lt;&sect;I(.+?)\&gt;#is';
	var_dump($regex); echo "<br /> <br />";

	$regex = html_entity_decode($regex);
	var_dump($regex); echo " --- html_entity_decode<br /> <br />";

	preg_match_all($regex, $str, $matches);
	var_dump($matches[0]); echo "<br /> <br />";
	var_dump($matches[1]); echo "<br /> <br />";

	$Nstringhe = count($matches[1]);
	var_dump($Nstringhe); echo "<br /> <br />";

	$replace = array();
	for ($i=0; $i<$Nstringhe; $i++)
		$replace[$i] = '&lt;'.CodificaLingua( $matches[1][$i] ).'&gt;';
	var_dump($replace); echo "<br /> <br />";

	return str_replace($matches[0], $replace, $str); 
}

Ho aggiunto questo! Ispirandomi al tuo :p

Codice:
    $str = html_entity_decode($str);

così fa proprio quello che volevo! grazie di tutto, ora lo metto in pratica e vediamo se va tutto bene XD
 

Veronica Auretta

Nuovo Utente
21 Apr 2015
25
0
0
Rieccomi, l'argomento è lo stesso... o comunque strettamente collegato, evito di aprire un altro thread. Io e le funzioni non andiamo olto d'accordo. Non so come ricercare esattamente questa espressione [§e § dove "e" è una lettera a caso dell'alfabeto e tra "e" e § c'è uno spazio. Ecco come ho provato a fare...

Codice:
<?php
 function trad_finale($str){
	$regex = "#\[§([.]{2})\§#is";
   preg_match_all($regex, $str, $matches);
	
    $Nstringhe = count($matches[1]); 
    $replace = array();
    for ($i=0; $i<$Nstringhe; $i++){	 
    $replace[$i] = '[§'.$matches[1][$i].']';
}
    return str_replace($matches[0], $replace, $str);	
	}
	
	function trad_esteso($str, $lingua){
	$regex = "#\[(.+?)\]#is";
   preg_match_all($regex, $str, $matches);
	
    $Nstringhe = count($matches[1]); 
    $replace = array();
    for ($i=0; $i<$Nstringhe; $i++){	 
    $replace[$i] = '[§'.$lingua.' '.$matches[1][$i].']';
}
    return trad_finale(str_replace($matches[0], $replace, $str));	
	}
	
$testo = "[lorem ipsum dolor sit amet] lorem ipsum dolor sit amet [§o lorem ipsum dolor sit amet]";
echo trad_esteso($testo,'e');

Il risultato sperato sarebbe
Codice:
[§e lorem ipsum dolor sit amete] lorem ipsum dolor sit amet [§o lorem ipsum dolor sit amet]
e invece è
Codice:
[§e lorem ipsum dolor sit amete] lorem ipsum dolor sit amet [§e §o lorem ipsum dolor sit amet]

...insomma come faccio a cercare [§e § e trasformarlo in [§ tenendo conto che la 'e' è un carattere a caso dell'alfabeto?

penso che sia questo sbagliato, ma, ripeto, non conosco bene la sintassi delle espressioni ):

Codice:
$regex = "#\[§([.]{2})\§#is";
 
Discussioni simili
Autore Titolo Forum Risposte Data
peppoweb L'OASIS al lavoro per codificare un linguaggio di sicurezza. Sicurezza e Virus 0
T Dividere un'immagine in 3 parti orizzontali e salvarle separatamente Photoshop 2
A Estrapolare parti di testo con PHP PHP 2
M Quali parti del pc affattica Photoshop? Photoshop 1
CssNewbie [HTML] Individuazione parti nel codice HTML e CSS 1
pup3770 Imposta-Suddividi pagina web in parti HTML e CSS 5
L Dividere classe in più parti PHP 3
Shyson Validare mail in varie parti Javascript 2
haringk Dividere ciclo in più parti PHP 12
MarcoGrazia Ma alcune parti del server sono secretate? Discussioni Varie 2
K creare pagine html con parti in comune [era:Consiglio tecnico] HTML e CSS 3
C dividere un'immagine in più parti Webdesign e Grafica 3
W Importare parti di file txt nel database PHP 0
M link fra diverse parti di un sito Flash 3
G Visualizzare parti di programma Leggi, Normative e Fisco 1
M Parti del phpbb sul proprio sito! phpBB 2
M pagina divisa in tre parti: come fare? HTML e CSS 7
GENZIANA Come Suddividere Una Immagine In Tante Parti? Photoshop 10
I salvare testo chat PHP 4
E Creare un testo trasparente dietro un div HTML e CSS 5
S da casella di testo a campo tabella Database 0
A Ottenere sfumatura su testo Photoshop 8
M Mostrare testo (o parte di esso) in base a utente PHP 0
F Animazione testo sito web diminuire grandezza di un testo allo scroll Javascript 0
D Rendere testo cliccabile PHP 3
D Casella di testo con grassetto ecc... HTML e CSS 2
Couting95 inserire dati da un file di testo in una tabella in php PHP 1
D Aiuto CSS in ELEMENTOR - Cambiare un testo CMS (Content Management System) 0
V Mailchimp - box di testo: cambia da solo il testo inserito Email Marketing 2
Barierta Testo a comparsa con passaggio del mouse Javascript 17
G grandezza testo HTML e CSS 4
D Testo colorato in base a giorno settimana PHP 12
S Testo scrolla su immagine che cambia HTML e CSS 0
S impostare un testo e una img nella stessa riga con jsPDF Javascript 0
R Nome input testo+variabile PHP 1
A Errore visualizzazione selezione testo Photoshop 0
Alex_70 Cerca testo all'interno di una stringa PHP 5
G Testo in mysql format 3 MySQL 0
G Box con testo casuale WordPress 1
G Modifica testo Photoshop 3
Y Colore sfondo testo Javascript 0
seranto [ASP] Controllare il testo inserito in Textarea Classic ASP 6
S [HTML] Effetto su testo da togliere HTML e CSS 0
A [WordPress] Recuperare testo articoli da sito danneggiato WordPress 1
A [HTML] Testo mail non visualizzabile su IOS HTML e CSS 0
R Bootstrap 4 - creare una finestra di testo responsive sopra un Carousel jQuery 1
Gabriele15497514 php testo errato durante la lettura del file txt quando lo script viene eseguito contemporaneamente PHP 3
I Creare Qsl radioamatore con testo editabile Presentati al Forum 1
Cosina [PHP] Cancellare una riga da un file di testo in base al nome PHP 2
Shyson [MySQL] Sostituire testo in in articolo MySQL 0

Discussioni simili