communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "string handling" - download "insert at specific location"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

string handling - insert at specific location was created/last modified on Sun 25 Apr 2004 7:43 PM

lets say you have a file where you want to insert a string into an existing file at a specific location

put a key into the file, where you want the new text appear

i.e.

<!--INSERT-->

for this to work this has to be on one line - and on its own

then use this snippet to insert the new data at this keypoint


<?

$insert_key
= '<!--INSERT-->';
$old_data = file('path/to/file');
$new_data = 'text to write in to the file';

for(
$x=0;$x<count($old_data);$x++)
{
     if(
$old_data[$x] != $insert_key)
     {
          
$string .= $old_data[$x];
      }else{
          
$string .= $insert_key."\n".$new_data."\n";
      }
}

// write $string to file


?>

learn more about some of the PHP functions used in this snippet: count, else, file, for, key

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari