PDA

View Full Version : [PHP] Come cancellare e cambiare una stringa???


pix83
08-09-2006, 00:11
Ammetto che non sono una cima nel programmare in PHP. Ho un piccolo problema. Devo inserire in una pagina un file HTM. Sembrerebbe tutto semplice...il comando che conosco io è:

<? include("statistiche/datigen.htm"); ?>

Il problema è che in questo file (datigen.htm) dovrei cambiare questo codice:

<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><center><table cellspacing='0' cellpadding='2' border='1'>

(che si ripete più volte nel file datigen.htm) con questa stringa:

<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><table class='marca' border= 1 cellspacing= 0 cellpadding= 2 >

Per poi stampare il tutto a video. Qualcuno sa gentilmente aiutarmi??? :mbe:

MEMon
08-09-2006, 11:46
datigen.htm da dove lo prendi? Cioè come lo crei perchè sinceramente non capisco come fai ad avere un problema simile :D se il file lo crei in qualche modo crealo già corretto! Risolvi il problema alla fonte!

Comunque tempo fa ti avevo già mostrato una cosa simile se non sbaglio, lavorando sulle stringhe, prova a ricercare il post, mi sembra che sia grosso modo la stessa cosa!

Xalexalex
08-09-2006, 11:54
eregi_replace("\n","",$stringa);

Yaoz

kk3z
08-09-2006, 17:54
Puoi fare:

function callback($buffer)
{
return (str_replace("<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><center><table cellspacing='0' cellpadding='2' border='1'>",
"<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><table class='marca' border= 1 cellspacing= 0 cellpadding= 2 >",
$buffer));
}

ob_start("callback");
include("statistiche/datigen.htm");
ob_end_flush();

oppure

$filecontent = file_get_contents("statistiche/datigen.htm");

$filecontent = str_replace("<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><center><table cellspacing='0' cellpadding='2' border='1'>", "<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><table class='marca' border= 1 cellspacing= 0 cellpadding= 2 >", $filecontent ));

echo $filecontent;

Xalexalex
08-09-2006, 18:00
Puoi fare:

function callback($buffer)
{
return (str_replace("<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><center><table cellspacing='0' cellpadding='2' border='1'>",
"<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><table class='marca' border= 1 cellspacing= 0 cellpadding= 2 >",
$buffer));
}

ob_start("callback");
include("statistiche/datigen.htm");
ob_end_flush();

oppure

$filecontent = file("statistiche/datigen.htm");

$filecontent = str_replace("<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><center><table cellspacing='0' cellpadding='2' border='1'>", "<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><table class='marca' border= 1 cellspacing= 0 cellpadding= 2 >", $filecontent ));

echo $filecontent;
La funzione file apre il file come array di righe.. Al massimo può usare file_get_contents... E il primo metodo non l'ho capito...

pix83
08-09-2006, 18:53
Puoi fare:

function callback($buffer)
{
return (str_replace("<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><center><table cellspacing='0' cellpadding='2' border='1'>",
"<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><table class='marca' border= 1 cellspacing= 0 cellpadding= 2 >",
$buffer));
}

ob_start("callback");
include("statistiche/datigen.htm");
ob_end_flush();

oppure

$filecontent = file("statistiche/datigen.htm");

$filecontent = str_replace("<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><center><table cellspacing='0' cellpadding='2' border='1'>", "<html><head><link href='fantamister.css' rel='stylesheet' type='Text/css'></head><body><table class='marca' border= 1 cellspacing= 0 cellpadding= 2 >", $filecontent ));

echo $filecontent;

kk3z puoi spiegarmi come funziona riga per riga perfavore? sono alquanto una cippa in PHP :(

kk3z
08-09-2006, 19:03
Il primo esempio l'ho copiato pari pari dalla documentazione php :read: : http://it2.php.net/ob_start . Nell'esempio si attiva l'output buffering prima di includere il file (ob_start()), praticamente tutto quello che viene output-tato :mbe: viene memorizzato in un buffer e non viene inviato. Quando chiami ob_end_flush() viene chiamata la funzione callback() che modifica quello che hai mandato in output, e flusha :muro: il buffer all'output.

Il secondo esempio con la correzione di Alex prende il contenuto del tuo file, sostituisce quello che si deve sostituire e lo outputta/echoa/printa :fagiano: .

:D

Alex, ora mi spieghi eregi_replace("\n","",$stringa)?

Xalexalex
09-09-2006, 11:30
Il primo esempio l'ho copiato pari pari dalla documentazione php :read: : http://it2.php.net/ob_start . Nell'esempio si attiva l'output buffering prima di includere il file (ob_start()), praticamente tutto quello che viene output-tato :mbe: viene memorizzato in un buffer e non viene inviato. Quando chiami ob_end_flush() viene chiamata la funzione callback() che modifica quello che hai mandato in output, e flusha :muro: il buffer all'output.

Il secondo esempio con la correzione di Alex prende il contenuto del tuo file, sostituisce quello che si deve sostituire e lo outputta/echoa/printa :fagiano: .

:D

Alex, ora mi spieghi eregi_replace("\n","",$stringa)?
Era un paucus una cazzata.. Cmq rimpiazza nel file tutti gli accapi con.. nulla :stordita: Va bene se prendi solo le righe che ti servono, per tutto il file proprio no...