Automatically replace title tag with H1 tag on 3000+ php files

Discussion in 'PHP' started by Dylan Tovey, Mar 26, 2008.

  1. #1
    I'm looking for a way to automatically replace a set of existing title tags on 3000 php files with the h1 tag on each file.

    Essentially - each file has a generic site title - but a file-specific H1 tag.

    Does anyone have any suggestions as to the best way to go about doing it? I only have basic php programming skills - so if it's super complex please let me know and I'll find someone to outsource it to.

    Alternatively - are there some existing scripts that would do the task?

    Thanks

    Dylan
     
    Dylan Tovey, Mar 26, 2008 IP
  2. Vbot

    Vbot Peon

    Messages:
    107
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I hope this is what you're looking for.

    Create a new file called findandreplace.php
    Then paste the following code in. Save and update it to your server.
    Make sure you change the path for the $dir according to your real path that content your 3000 php files.
    Warning: Make sure you backup your files first. I'm not responsability for any damages to your files. Use it at your own risk.

    <?php
    $dir = '.';
    $count = 0;
    $handle = opendir($dir);
    while($file = readdir($handle))
    {
    	if(substr($file,-4) == '.php' && $file != basename($_SERVER['PHP_SELF']))
    	{
    		$data = file_get_contents($dir.'/'.$file);
    		if(preg_match("#<title>(.+?)</title>#is", $data))
    		{
    			$count++;
    			$data = preg_replace("#<title>(.+?)</title>#is", "<h1>\\1</h1>", $data);
    			$fp = fopen($dir.'/'.$file, 'w');
    			fwrite($fp, $data);
    			fclose($fp);
    		}
    	}
    }
    if($count > 0) echo $count." Files replaced.";
    ?>
    PHP:
     
    Vbot, Mar 27, 2008 IP
    Dylan Tovey likes this.
  3. Dylan Tovey

    Dylan Tovey Peon

    Messages:
    380
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think this should work :)

    Hugely appreciated.

    I spent hours trying to work out how to do it myself - and this is much sleeker (and smaller)

    Thanks again.

    Dylan
     
    Dylan Tovey, Mar 27, 2008 IP