Jump to content


Photo

Problemas Com Str_replace()...


  • Faça o login para participar
7 replies to this topic

#1 gURu

gURu

    Novato no fórum

  • Usuários
  • 24 posts
  • Sexo:Não informado

Posted 08/02/2005, 11:41

Bom estou escrevendo um conversor de strings para decimal (nem me perguntem porque =P ). Olhando no LookUpTables consegui todos os valores ( o decimal fica %252echr(x), x é o valor que está no lookuptables)

O problema é que o str_replace em vez de substituir somente os caracteres da string dada ele tambem substitui os caracteres ja substituidos, ou seja:

Digamos que a string dada é:

Oi, tudo bem?

Ela deveria ficar:

%252echr(79)%252echr(105)%252echr(44)%252echr(32)%252echr(116)%252echr(117)%252echr(100)%252echr(111)%252echr(32)%252echr(98)%252echr(101)%252echr(109)%252echr(63)

Mas como cada caractere ja substituido é substituido de novo fica:

Não caberia num post por isso não botei mas acho que vocês ja tem a ideia :P

Bom gostaria de que os str replaces mudassem na primeira variavel e não na mais recente...

Aqui o codigo da minha pagina: (não está completa eu parei quando precebi que não ia funcionar)

<?php
$text = $_POST['text'];
if (!empty ($text)) {
$text = str_replace(' ', '%252echr(32)', $text);
$text = str_replace('!', '%252echr(33)', $text);
$text = str_replace('"', '%252echr(34)', $text);
$text = str_replace('#', '%252echr(35)', $text);
$text = str_replace('$', '%252echr(36)', $text);
$text = str_replace('%', '%252echr(37)', $text);
$text = str_replace('&', '%252echr(38)', $text);
$text = str_replace("'", '%252echr(39)', $text);
$text = str_replace('(', '%252echr(40)', $text);
$text = str_replace(')', '%252echr(41)', $text);
$text = str_replace('*', '%252echr(42)', $text);
$text = str_replace('+', '%252echr(43)', $text);
$text = str_replace(',', '%252echr(44)', $text);
$text = str_replace('-', '%252echr(45)', $text);
$text = str_replace('.', '%252echr(46)', $text);
$text = str_replace('/', '%252echr(47)', $text);
$text = str_replace('0', '%252echr(48)', $text);
$text = str_replace('1', '%252echr(49)', $text);
$text = str_replace('2', '%252echr(50)', $text);
$text = str_replace('3', '%252echr(51)', $text);
$text = str_replace('4', '%252echr(52)', $text);
$text = str_replace('5', '%252echr(53)', $text);
$text = str_replace('6', '%252echr(54)', $text);
$text = str_replace('7', '%252echr(55)', $text);
$text = str_replace('8', '%252echr(56)', $text);
$text = str_replace('9', '%252echr(57)', $text);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Text to Decimal</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>

<body>

<form method="post" action="<?php echo $PHP_SELF;?>">
Input: <input type="text" name="text" size="20">
<input type="submit" value="submit" name="submit">
</form>

<?
if (isset($_POST['submit'])) {
print "<textarea name='body' rows='8' cols='25'>$text</textarea>";
}
?>

</body>
</html>


#2 Felipe Pena

Felipe Pena

    O temor do Senhor é o princípio da sabedoria

  • Ex-Admins
  • 6441 posts
  • Sexo:Masculino

Posted 08/02/2005, 13:18

<?php
$text = $_POST['text'];

if (!empty($text)) {

  for ($i=0;$i<strlen($text);$i++) {
  echo $text[$i];
 
      if ($text[$i]==" ") $ntext .= "%252echr(32)";
      if ($text[$i]=="!") $ntext .= "%252echr(33)";
      if ($text[$i]=="\"") $ntext .= "%252echr(34)";
      if ($text[$i]=="#") $ntext .= "%252echr(35)";
      if ($text[$i]=="$") $ntext .= "%252echr(36)";
      if ($text[$i]=="%") $ntext .= "%252echr(37)";
      if ($text[$i]=="&") $ntext .= "%252echr(38)";
      if ($text[$i]=="'") $ntext .= "%252echr(39)";
      if ($text[$i]=="(") $ntext .= "%252echr(40)";
      if ($text[$i]==")") $ntext .= "%252echr(41)";
      if ($text[$i]=="*") $ntext .= "%252echr(42)";
      if ($text[$i]=="+") $ntext .= "%252echr(43)";
      if ($text[$i]==",") $ntext .= "%252echr(44)";
      if ($text[$i]=="-") $ntext .= "%252echr(45)";
      if ($text[$i]==".") $ntext .= "%252echr(46)";
      if ($text[$i]=="/") $ntext .= "%252echr(47)";
      if ($text[$i]=="0") $ntext .= "%252echr(48)";
      if ($text[$i]=="1") $ntext .= "%252echr(49)";
      if ($text[$i]=="2") $ntext .= "%252echr(50)";
      if ($text[$i]=="3") $ntext .= "%252echr(51)";
      if ($text[$i]=="4") $ntext .= "%252echr(52)";
      if ($text[$i]=="5") $ntext .= "%252echr(53)";
      if ($text[$i]=="6") $ntext .= "%252echr(54)";
      if ($text[$i]=="7") $ntext .= "%252echr(55)";
      if ($text[$i]=="8") $ntext .= "%252echr(56)";
      if ($text[$i]=="9") $ntext .= "%252echr(57)";
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR...nsitional.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Text to Decimal</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>

<body>

<form method="post" action="<?php echo $PHP_SELF?>">
Input: <input type="text" name="text" size="20">
<input type="submit" value="submit" name="submit">
</form>

<?
if (isset($_POST['submit'])) {
  echo "<textarea name='body' rows='20' cols='25'>".$ntext."</textarea>";
}
?>

</body>
</html>


Felipe Pena
[...] ó terra, terra, terra; ouve a palavra do Senhor. — Jeremias 22:29

#3 gURu

gURu

    Novato no fórum

  • Usuários
  • 24 posts
  • Sexo:Não informado

Posted 08/02/2005, 13:50

Aew cara valew mesmo, eu nunca tinha pensando que um loop pudesse fazer isso :P Vo da uma estudada melhor neles.

Edição feita por: gURu, 08/02/2005, 13:51.


#4 gURu

gURu

    Novato no fórum

  • Usuários
  • 24 posts
  • Sexo:Não informado

Posted 08/02/2005, 15:47

Olha eu passei tudo e funciono perfeitamente só nessa linha deu problema:

if ($text[$i]=="\") $ntext .= "%252echr(92)";

Apareceu:

Parse error: parse error, unexpected T_STRING in /usr/local/apache2/htdocs/bruno/dec.php on line 68

Quando eu tiro aquela linha o erro some...

#5 Felipe Pena

Felipe Pena

    O temor do Senhor é o princípio da sabedoria

  • Ex-Admins
  • 6441 posts
  • Sexo:Masculino

Posted 08/02/2005, 16:01

Pois é...

A forma correta:

if ($text[$i]=="\\") $ntext .= "%252echr(92)";



[]'s
Felipe Pena
[...] ó terra, terra, terra; ouve a palavra do Senhor. — Jeremias 22:29

#6 gURu

gURu

    Novato no fórum

  • Usuários
  • 24 posts
  • Sexo:Não informado

Posted 08/02/2005, 16:14

Aew valew mesmo... Sem querer abusar mas sera que voce podia me dizer porque daquela forma estava errada e dessa certa?

#7 Felipe Pena

Felipe Pena

    O temor do Senhor é o princípio da sabedoria

  • Ex-Admins
  • 6441 posts
  • Sexo:Masculino

Posted 08/02/2005, 16:22

Porque \" entre aspas significa aspas.

Exemplo:

<?
echo "Testando as \" aspas \" ! ";
?>


Para representar então uma \ (barra inversa) utiliza-se \\ (duas barras inversas).

[]'s
Felipe Pena
[...] ó terra, terra, terra; ouve a palavra do Senhor. — Jeremias 22:29

#8 gURu

gURu

    Novato no fórum

  • Usuários
  • 24 posts
  • Sexo:Não informado

Posted 08/02/2005, 16:42

Vlw :)




1 user(s) are reading this topic

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq