Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Read a file and rewrite its contents...

    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?

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,078
    Thanks
    11
    Thanked 98 Times in 96 Posts
    for simple stuff like <br> to <br /> etc then str_replace() would be easy

    PHP Code:
    <?
    $file 
    implode'' file$filename ) ) ;
    $file str_replace
        array( 
    '<br>','<hr>','etc') ,
        array( 
    '<br />','<hr />','<etc />') ,
        
    $file
    );
    $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
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What "more complex" situations would you forsee? Wouldn't globally replacing " />" with ">" do the trick?

  • #4
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What about
    PHP Code:
    $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.
    Last edited by trib4lmaniac; 10-04-2004 at 07:30 PM.

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,078
    Thanks
    11
    Thanked 98 Times in 96 Posts
    Quote Originally Posted by trib4lmaniac
    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.
    Last edited by firepages; 10-05-2004 at 02:25 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •