I'm fiddling with a database of a friend who has had some data entered wrong or a bit confusing or it's me that's making it difficult.
Ex: Mariana "Gurizinha's"
This example was saved to the database as follows: \'Gurizinha\'s\'
To display on his website I tried to create a function which replaces the initial character with a "and the ending also with".
The right one would be to get text that was saved in this way and that has 100 words or more and correct displaying: "Gurizinha's"
instead of \'Gurizinha\'s\'
.
If I use str_replace
it will get all the text and what it has \ 'going to change to "so if it is words that need Are's it will replace with ".
function rt_especial($valor) {
$i = 0;
$palavras = explode(' ', $valor);
foreach($palavras as $Arr) {
$i++;
$novotexto = stripcslashes($Arr);
$i2 = 0;
$palavras2 = explode("'", $novotexto);
foreach($palavras2 as $Arr2) {
$i2++;
}
for($i3 = 0; $i3 <= $i2; $i3++) {
$str = '"';
$str .= $palavras2[$i3];
}
}
echo substr($string, -1);
$novotexto = join(' ', $novotexto);
return $novotexto;
}
Code that inserts data into MySQL database:
function db_executa($tabela, $dados, $acao = 'insert', $parametros = '') {
reset($dados);
if (strtolower($acao) == 'insert') {
$consulta = 'insert into ' . $tabela . ' (';
while (list($coluna, ) = each($dados)) $consulta .= $coluna . ', ';
$consulta = substr($consulta, 0, -2) . ') values (';
reset($dados);
while (list(, $valor) = each($dados)) {
switch ((string)$valor) {
case 'now()':
$consulta .= 'now(), ';
break;
case 'null':
$consulta .= 'null, ';
break;
default:
$consulta .= '\'' . db_entrada($valor) . '\', ';
break;
}
}
$consulta = substr($consulta, 0, -2) . ')';
} elseif (strtolower($acao) == 'update') {
$consulta = 'update ' . $tabela . ' set ';
reset($dados);
while (list($coluna, $valor) = each($dados)) {
switch ((string)$valor) {
case 'now()':
$consulta .= $coluna . ' = now(), ';
break;
case 'null':
$consulta .= $coluna .= ' = null, ';
break;
default:
$consulta .= $coluna . ' = \'' . db_entrada($valor) . '\', ';
break;
}
}
$consulta = substr($consulta, 0, -2) . ' where ' . $parametros;
}
return db_consulta($consulta);
}
Problem part resolution: It is not a beautiful way, much less certain, but that is what I managed to solve.
function rt_especial($valor){
$string = $valor;
$separa = explode(" ", $string); // quebra a string nos espaços
$count = count($separa); // quantidade de separações
$arrayok = array();
for($i=0; $i<= $count; $i++)
{
// Pego toda palavra que começa com \' e substituo por "
$string2 = ereg_replace("^([/\'])", '"',$separa[$i]);
$string3 = str_replace("\',", '",', $string2);
$string4 = str_replace("\',", '",', $string3);
$string5 = ereg_replace('^([/""])', '"',$string4);
$string6 = ereg_replace('([/""])$', '"',$string5);
//Pego toda palavra que termina com \' e substituo por "
$string = ereg_replace("([/\'])$", '"',$string6);
$string7 = str_replace('"\'', '"', $string);
$string8 = str_replace("\'\"", '"', $string7);
$string9 = str_replace('\"', '"', $string8);
$arrayok[$i] = $string9;
}
$ccp = implode(' ', $arrayok);
return $ccp;
}