View Full Version : Read a file and rewrite its contents...

10-04-2004, 05:46 PM
Let's say that I am insane and want to rewrite a file so that all self-terminating elements are marked up as XML when the page is served as such, and marked up as HTML otherwise...

Is this feasible with PHP alone, or would I be looking at using server software as well?

10-04-2004, 07:11 PM
for simple stuff like <br> to <br /> etc then str_replace() would be easy

$file = implode( '' , file( $filename ) ) ;
$file = str_replace(
array( '<br>','<hr>','etc') ,
array( '<br />','<hr />','<etc />') ,
$fp = fopen( 'copy_of_' . $filename , 'w' ) ;
fputs ( $fp , $file ) ;
fclose( $fp );

for more complex validation perhaps look at the the PECL HTMLtidy extension http://pecl.php.net/package/tidy

10-04-2004, 07:17 PM
What "more complex" situations would you forsee? Wouldn't globally replacing " />" with ">" do the trick?

10-04-2004, 07:25 PM
What about

$file = file_get_contents($filename);
$single_tags = array('br', 'hr', 'etc');
foreach($single_tags as $tag) {
$file = preg_replace('#<(' . $tag . '.*?)>#ms', '<\1 />', $file);

Which would put />'s at the end instead of remove them. Would be about the same for the other way.

10-05-2004, 02:20 AM
Which would put />'s at the end instead of remove them.
thats what the first sample does ?

I like to avoid regex if possible as str_replace is much faster , of course you can do more with regex , e.g. with regex you could check for <b>no trailing space</b> and add it

|V|[agnus] What "more complex" situations would you forsee?
I dunno, you mentioned self-closing tags but I don't know if you had other operations to perform so just giving you that link for reference.
err, an example might be list items with no closing </li> which many a 'html editor' might miss or the <b> example above.

10-05-2004, 03:50 AM
Yes, I thought that(going beyond self-terminating elements) was what you were getting at after I posted that. :) Thanks again. Good random info about str_replace vs. preg_replace, too, and it figures, I guess. :thumbsup: