Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

renandare

Php + json

Recommended Posts

Ae galera,

 

Estou com um problema com json + php...

Sou programador asp e sempre uso json para fazer paginação de registros..

 

Porém o json precisa de uma string ( para quem não sabe ) dessa forma:

 

["{"nome":"Renan","sexo":"Masculino","Atuacao":"Programador"},{"nome":"Renan","sexo":"Masculino","Atuacao":"nenhuma"}"]
e assim vai...

o problema é o seguinte:

 

O json não interpreta Acentos, então em asp eu sempre uso uma funçao para "encodar" o conteudo do que vai pro json, sendo assim ele "encoda" tudo qto é tag html ( <br />, vbcrl, etc ) e não deixa com que a string que retorna quebre a string json ( o json tem que receber uma string "tripa" igual postei a cima se tiver algo ( <br/> por ex ) quebrando a linha por ex ele não funciona. )

Agora o meu problema é o seguinte, como fazer isso com php ?

tentei htmlentites(), encode(), e várias outras funções mais nenhuma me resolveu o problema, acho que com encode() eu até consegui mais ele encodou tudo, e na hr de apresentar o resultado ele apresentou tudo "encodado".

 

Não sei se consegui ser bem claro, mas caso alguem não entendeu mto bem o que preciso pergunta melhor ai :P

 

Resumindo: preciso saber como "encodar" uma string html sem estragar o resultado na tela, ou então "encodar" dps "desencodar" ela.

 

abraços!

Compartilhar este post


Link para o post
Compartilhar em outros sites

sobre os acentos você pode usar utf8_encode na string que tem acento

 

depois usar normalmente json_encode no array final

Então, mais ai que tá preciso que o php ignore as tags html na hr de passar pro json.... e não só os acentos... td.. <br /> etc

Compartilhar este post


Link para o post
Compartilhar em outros sites

Dae galera ninguém?

To com um problema em fazer o meu php ler o conteudo do banco, e tirar o Chr(13)...

 

acontece que meu php não tá "entendendo" que no retorno do banco, tem uma quebra de linha..

 

estou usando duas funções, uma para encodar a string, e outra que seria exatamente para pegar o chr, segue:

 

 

Função para tirar o chr(13) (quebra de linha)

unction SubAcento($variavel){

$chh=1;
while($chh!=0){

	$chh = strpos($variavel, chr(13));
	
	$tam = strlen($variavel);
	
	if($chh>0){
		$variavel = substr($variavel,0,$chh).substr($variavel,$chh+2,$tam);
	}
	}

	return $variavel;
}

Função para encodar a string.

 

function SubAcento2($REMOVE_ACENTOS){
   
$REMOVE_ACENTOS = str_replace( "à", "à", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "á", "á", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "â", "â", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ã", "ã", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ä", "ä", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "è", "è", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "é", "é", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ê", "ê", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ë", "ë", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "ì", "ì", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "í", "í", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "î", "î", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ï", "ï", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "ò", "ò", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ó", "ó", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ô", "ô", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "õ", "õ", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ö", "ö", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "ù", "ù", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ú", "ú", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "û", "û", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ü", "ü", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "À", "À", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Á", "Á", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Â", "Â", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ã", "Ã", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ä", "Ä", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "È", "È", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "É", "É", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ê", "Ê", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ë", "Ë", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "Ì", "Ì", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Í", "Í", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Î", "Î", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ï", "Ï", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "Ò", "Ò", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ó", "Ó", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ô", "Ô", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Õ", "Õ", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ö", "Ö", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "Ù", "Ù", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ú", "Ú", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Û", "Û", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ü", "Ü", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "ç", "ç", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ç", "Ç", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "ñ", "ñ", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ñ", "Ñ", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "´", "´", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "`", "`", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "¨", "¨", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "^", "ˆ", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "~", "˜", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "\"", """, $REMOVE_ACENTOS);

return $REMOVE_ACENTOS;
}

 

e uso da seguinte forma o resultado da consulta do banco...

 

$Opiniao = SubAcento2($rs['Opi_Opiniao']);

$Opiniao = SubAcento($Opiniao);

 

porém não está dando certo, não sei pq...testei a função passando uma string qualquer com <br> dentro dela, e funcionou beleza....

mais na hr de pegar o resultado do banco igual tá acima aí ele não reconhece os <br />, chr(13) e acaba quebrando as linhas...

 

 

alguem sabe oq pode ser?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Nossa que gambiarra que você fez

 

pra eliminar a quebra de linha nao tem segredo basta localizar o \n e substituir por "" ou expressao regular

 

echo preg_replace('/\s\s+/', '', $texto);

Compartilhar este post


Link para o post
Compartilhar em outros sites

Nossa que gambiarra que você fez

 

pra eliminar a quebra de linha nao tem segredo basta localizar o \n e substituir por "" ou expressao regular

 

echo preg_replace('/\s\s+/', '', $texto);

entao cara ...

o problema é justamente esse, eu não consigo achar a quebra de linha...

posso colocar este codigo que se eu tiver um retorno por com quebra de linha ele quebra da mesma forma...

 

segue o jeito que estou fazendo :

 

echo 'Nome:"'.$Opi_Nome.'",';
echo 'Area:"'.$Opi_Areaatuacao.'",';
echo 'Cidade:"'.$Opi_Cidade.'"';
echo 'Opiniao:"'.preg_replace('/\s\s+/', '', $rs['Opi_Opiniao']). '",';

Note que tentei da forma que você falou porém mesmo assim no meu código fonte meu php não tirou a quebra de linha..veja :

 

[{Nome:"REnan",Area:"ae ",Cidade:"ae"Opiniao:"ae
ae
ae
ae
ae
ae
ae
",Nome:"Sapo",Area:"Designer",Cidade:"Curitiba"Opiniao:"Oi meu nome é Sapo, e eu achei muito legal a programação do Renanzinho, ele é gente fina :)",}]

E daí alguém tem uma sugestão de como arrumar?

Compartilhar este post


Link para o post
Compartilhar em outros sites

nao precisa de outra solução você deve estar fazendo outra coisa errada, posta seu codigo pra gente ver.

<?
include "includes/conexao.php"; // Conecta ao banco de dados
include "includes/ExecutaQuery.php"; // Executa a cláusula SQL

function SubAcento($variavel){

$chh=1;
while($chh!=0){

	$chh = strpos($variavel, chr(13));
	
	$tam = strlen($variavel);
	
	if($chh>0){
		$variavel = substr($variavel,0,$chh).substr($variavel,$chh+2,$tam);
	}
	}

	return $variavel;
}

function SubAcento2($REMOVE_ACENTOS){
   
$REMOVE_ACENTOS = str_replace( "à", "à", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "á", "á", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "â", "â", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ã", "ã", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ä", "ä", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "è", "è", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "é", "é", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ê", "ê", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ë", "ë", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "ì", "ì", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "í", "í", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "î", "î", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ï", "ï", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "ò", "ò", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ó", "ó", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ô", "ô", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "õ", "õ", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ö", "ö", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "ù", "ù", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ú", "ú", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "û", "û", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "ü", "ü", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "À", "À", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Á", "Á", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Â", "Â", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ã", "Ã", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ä", "Ä", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "È", "È", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "É", "É", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ê", "Ê", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ë", "Ë", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "Ì", "Ì", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Í", "Í", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Î", "Î", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ï", "Ï", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "Ò", "Ò", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ó", "Ó", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ô", "Ô", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Õ", "Õ", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ö", "Ö", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "Ù", "Ù", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ú", "Ú", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Û", "Û", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ü", "Ü", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "ç", "ç", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ç", "Ç", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "ñ", "ñ", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "Ñ", "Ñ", $REMOVE_ACENTOS);

$REMOVE_ACENTOS = str_replace( "´", "´", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "`", "`", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "¨", "¨", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "^", "ˆ", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "~", "˜", $REMOVE_ACENTOS);
$REMOVE_ACENTOS = str_replace( "\"", """, $REMOVE_ACENTOS);

return $REMOVE_ACENTOS;
}

$sql = "select * from opinioes o left join estados e on o.Opi_Estado = e.Est_Id where o.Opi_Status = 1 and o.Opi_Autorizacao = 1";

echo '[';

$res = ExecutaQuery($id,$sql);
$linhas = mysql_num_rows($res);
for($i=0;$i<$linhas;$i++) {
	
	$rs = mysql_fetch_array($res);
	
	if($i=='0'){
		echo '{';
	}
	
		
	//$Opiniao			= SubAcento2($rs['Opi_Opiniao']);
	$Opi_Nome 			= SubAcento2($rs['Opi_Nome']);
	$Opi_Areaatuacao	= SubAcento2($rs['Opi_Areaatuacao']);
	$Opi_Cidade			= SubAcento2($rs['Opi_Cidade']);
	
	//$Opiniao 			= SubAcento($Opiniao);
	$Opi_Nome 			= SubAcento($Opi_Nome);
	$Opi_Areaatuacao 	= SubAcento($Opi_Areaatuacao);
	$Opi_Cidade			= SubAcento($Opi_Cidade);
	$Opiniao = SubAcento2($rs['Opi_Opiniao']);
	$Opiniao = str_replace("\n","<br />",$Opiniao);	
	
	//testar se precisa disso :)
	$Opiniao = SubAcento($Opiniao);
	
	echo 'Nome:"'.$Opi_Nome.'",';
	echo 'Area:"'.$Opi_Areaatuacao.'",';
	echo 'Cidade:"'.$Opi_Cidade.'"';
	echo 'Opiniao:"'.$Opiniao.'"';
	
	//$rs = mysql_fetch_array($res)
	//echo("<font size='7'>$i - $linhas</font>\n");
	
	if($i==$linhas-1){
		echo '}';
	}
	
}
echo ']';

?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Entao sua função REMOVE_ACENTOS nao ta removendo os acentos só esta transformando eles em HTML entities

 

pra isso nao precisa fazer toda aquela gambiarra só usar a função htmlentities()

 

outra coisa se quer mandar o resultado em json use json_encode();

 

e para eliminar as quebras de linhas basta fazer o que eu ja postei

 

mas teste e veja como ta chegando os dados, olhe o codigo fonte da pagina, porque se ja tiver vindo com <br />, dai nao funciona mesmo

no caso teria que eliminar os <br />

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.