Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Galera,
como eu faço para substituir o valor de um link(href) salvo no banco de dados?
Exemplo:
Tenho este código salvo no banco de dados:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<custom name="opencounter" type="tracking">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body bgcolor="#FFFFFF" style="margin:10px;">
<table width="600" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="45" height="28" bgcolor="#f5f5f5"> </td>
<td width="508" height="28" align="center" bgcolor="#f5f5f5"><span style="font-size:11px;color:#000000;font-family: Arial;"> Caso
não esteja visualizando corretamente esta mensagem, <a style="text-decoration:none;color:#000000;font-weight:bold;" title="esse link" href="%%view_email_url%%" alias="esse link" conversion="false">acesse aqui.</a> </span></td>
<td width="47" height="28" bgcolor="#f5f5f5"> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="45" height="49" bgcolor="#f5f5f5"> </td>
<td width="508" height="49"><a href="[http://linkX](http://linkX)"><img src="img" width="508" height="49" alt="imagem" style="display:block;border: none;" /></a>
</td>Tentei com o str_replace() mas não rolou muito bem.
Meu código hoje está assim:
$html = $html[0]['html_code'];
echo $html;die();
for ($i=0; $i<=(count($dest)-1); $i++){
$linkAntigo = htmlspecialchars($dest[$i]['main_url']);
echo $linkAntigo."<br >";
$html = str_replace('href="'.$linkAntigo.'"',$dest[$i]['new_url'].$i,$html);
}
echo $html;
Alguma luz?
Todos.
Explicando melhor pq percebi que escrevi e nao expliquei muito bem.
Galera,
preciso substituir todos os href de um html que é salvo no banco de dados.
Todos os links do html foram capturados e salvos em outra tabela no banco.
Foi também gerado novos links que irão substituir os originais.
Nesse caso temos:
tabela_com_html
html_code
tabela_de_links
url_original
url_nova
O que preciso é que o script leia todo o código html, identifique os links originais e troque pelos novos correspondentes.
Exemplo:
Esse é o html salvo no banco de dados
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html> <head> <custom name="opencounter" type="tracking"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body bgcolor="#FFFFFF" style="margin:10px;"><table width="600" border="0" cellpadding="0" cellspacing="0" align="center"> <tr><td> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td width="45" height="28" bgcolor="#f5f5f5"> </td> <td width="508" height="28" align="center" bgcolor="#f5f5f5"><span style="font-size:11px;color:#000000;font-family: Arial;"> Casonão esteja visualizando corretamente esta mensagem, <a style="text-decoration:none;color:#000000;font-weight:bold;" title="esse link" href="%%view_email_url%%" alias="esse link" conversion="false">acesse aqui.</a> </span></td> <td width="47" height="28" bgcolor="#f5f5f5"> </td> </tr> </table> </td></tr> <tr><td> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td width="45" height="49" bgcolor="#f5f5f5"> </td> <td width="508" height="49"><a href="http://linkX"><img src="img" width="508" height="49" alt="imagem" style="display:block;border: none;" /></a> </td>
Esse é o código que eu fiz tentando fazer esse milagre...não rolou, claro.
$html = $html[0]['html_code']; echo $html;die(); for ($i=0; $i<=(count($dest)-1); $i++){ $linkAntigo = htmlspecialchars($dest[$i]['main_url']); echo $linkAntigo."<br >"; $html = str_replace('href="'.$linkAntigo.'"',$dest[$i]['new_url'].$i,$html); } echo $html;
Alguma luz?Cara estou com o mesmo problema! Eu quase achei um meio de fazer usando Recursividade mas, acho que tem um jeito melhor de fazer porém já pesquisei mas não encontro nada :c
Eu estou tentando utilizando o preg_replace_callback dentro de uma função que chama a si mesma. Porém as vezes o regex que eu uso não encontra o <a href... E de vez enquando encontra.
Qual href? Tem mais de 1 no código. Especifique.