Page 1 sur 1

Rajouter un include_once.. ou bout de code...dans php existant..

Posté : 12 déc. 2018, 18:14
par harmony
Bonjour a tous ,j'ai un tit soucis
voudrait rajouter un include_once("toto.inc.php"); dans un php existant.
me demandais si quelque un pourrait me donner un coup de pouce..
je suis tres mauvais en php donc...
voici le code que je voudrait rajouter le include_once("toto.inc.php");
php existant test.php
  <?php
			$css .= 'header{background-size:' . $this->getData(['theme','header','imageContainer']).'}';
			if($themeHeaderImage = $this->getData(['theme', 'header', 'image'])) {
			$css .= 'header{background-image:url("../file/source/' . $themeHeaderImage . '");background-position:' . $this->getData(['theme', 'header', 'imagePosition']) . ';background-repeat:' . $this->getData(['theme', 'header', 'imageRepeat']) . '}';	
			}
			$colors = helper::colorVariants($this->getData(['theme', 'header', 'textColor']));
?>
voudrait que ça ressemble a ça au final just rajouter include_once("toto.inc.php");
  <?php
			$css .= 'header{background-size:' . $this->getData(['theme','header','imageContainer']).'}';
			if($themeHeaderImage = $this->getData(['theme', 'header', 'image'])) {
			$css .= 'header{background-image:url("../file/source/' . $themeHeaderImage . '");background-position:' . $this->getData(['theme', 'header', 'imagePosition']) . ';background-repeat:' . $this->getData(['theme', 'header', 'imageRepeat']) . '}';	
			}
			include_once("toto.inc.php");/////////////////////////////////ICI/////////////////////
			$colors = helper::colorVariants($this->getData(['theme', 'header', 'textColor']));
?>
le code que j ai essayer d'utiliser pour modifier c'est ça
  <?php
$file = $_SERVER['DOCUMENT_ROOT'] .'/test.php';
$filecontent=file_get_contents($file);
$replace=str_replace('ici je sais pas quoi rajouter', 'include_once("toto.inc.php");', $filecontent );
file_put_contents($file, $replace);
?>
je sais pas si l’on peut le faire avec ça ?
merci d'avance

Re: Rajouter un include_once.. ou bout de code...dans php existant..

Posté : 13 déc. 2018, 00:08
par @rthur
Un truc du genre :
$replace=str_replace("$colors = helper::colorVariants", "include_once('toto.inc.php');\n$colors = helper::colorVariants", $filecontent );
2 choses à noter :
- le retour à la ligne se fait grace aux caractère "\n"
- pour que "\n" remplace un retour à la ligne, il faut qu'il soit entre guillemets doubles, c'est pour cette raison que j'ai inversé le sens de tes guillemets : doubles pour délimiter la chaine de caractères et simple autour du nom du fichier toto.inc.php

Re: Rajouter un include_once.. ou bout de code...dans php existant..

Posté : 13 déc. 2018, 01:05
par harmony
coucou @rthur grand merci j'ai bien compris , je me suis permis de faire une modif car il y avait quelques erreurs je sais pas si j'ai bien fait mais ça fonctionne .
  <?php
$replace=str_replace('$colors = helper::colorVariants', "include_once('toto.inc.php');\n\$colors = helper::colorVariants", $filecontent );
?>
A toutes l’équipe PHPFRANCE qui répondent à des questions,problèmes....
Vous rendez les personnes heureuses .. alors grand MERCI