SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php regex question

    Hi,

    I have created a script to read and write to a file. The problem I am having is that I am trying to replace the software version in the following line:

    PHP Code:
    define('SOFTWARE_VERSION','2.5'); 
    What is the regex I need to replace the 2.5 with the new version number?

  2. #2
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand you correctly, you want to replace "2.5" in the string "define('SOFTWARE_VERSION','2.5');" with another version number?

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by W1LL View Post
    If I understand you correctly, you want to replace "2.5" in the string "define('SOFTWARE_VERSION','2.5');" with another version number?
    Yes, thats correct.

  4. #4
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $phpCode 
    = <<< PHP
    <?php
    define('SOFTWARE_VERSION','2.5');
    ?>
    PHP;

    $newVersion '5.0.1';

    $phpCode preg_replace('/define\(\'SOFTWARE_VERSION\',\'(\d+\.\d+)\'\);/''define(\'SOFTWARE_VERSION\',\'' $newVersion '\');'$phpCode);
    ?>
    That should do it!

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by W1LL View Post
    PHP Code:
    <?php
    $phpCode 
    = <<< PHP
    <?php
    define('SOFTWARE_VERSION','2.5');
    ?>
    PHP;

    $newVersion '5.0.1';

    $phpCode preg_replace('/define\(\'SOFTWARE_VERSION\',\'(\d+\.\d+)\'\);/''define(\'SOFTWARE_VERSION\',\'' $newVersion '\');'$phpCode);
    ?>
    That should do it!
    Hi, thanks for your help on this. I just tried the regex you posted in my code below, but the preg_match does not find the line. Any ideas? Here's the code:

    PHP Code:
    function UpdateConfig(){
            
    // Define file
            
    $file DIR.'/includes/config2.php';
            
    // Define pattern to replace
            
    $pattern '/define\(\'SOFTWARE_VERSION\',\'(\d+\.\d+)\'\);/';
            
    // New version
            
    $newversion '2.5.1';
            
            if(!
    file_exists($file)) {
                return 
    false;
            } else {
                
    $array file($file);
                
    $content;
                for(
    $i=0;$i<count($array);$i++) {
                    if(
    preg_match($pattern$array[$i])) {
                        
    $content .= preg_replace($pattern'define(\'SOFTWARE_VERSION\',\''.$newversion.'\');'$content);
                    } else {
                        
    $content .= $array[$i];
                    }
                }
            }
            
            
    // Open file
            
    $newfile fopen($file"w");
            
    // Rewrite content
            
    fwrite($newfile$content);
            
    // Close file
            
    fclose($newfile);
        } 

  6. #6
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change line 16 to:

    PHP Code:
    $content .= preg_replace($pattern'define(\'SOFTWARE_VERSION\',\'' $newversion '\');'$array[$i]); 

  7. #7
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by W1LL View Post
    Change line 16 to:

    PHP Code:
    $content .= preg_replace($pattern'define(\'SOFTWARE_VERSION\',\'' $newversion '\');'$array[$i]); 
    Still doesn't work. I think it's something to do with the preg_match, but can't figure it out

  8. #8
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh managed to figure it out...needed a space after the comma in the regex.

    Thanks!

  9. #9
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha OK, good stuff!


Bookmarks

Posting Permissions

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