Thread: Regex help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    22
    Rep Power
    0

    Regex help


    hello guys i am trying to preg_replace this line --> $parts = preg_split("/\.\(.*\)\.\(.*\)/", $file);

    The bold characters ".*" is what i am intending to replace with numbers and strings...i hope you guys can help me..

    This is the closest i can get to
    $batch_contents = preg_replace('~(preg_split\("/\\\\.\\\\(\.\*\\\\)\\\\.\\\\()([0-9.]+)(\\\\))~i', '${1}'.$newArgument, $batch_contents);
  2. #2
  3. Autodidact
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Location
    Canada
    Posts
    741
    Rep Power
    833
    Post example strings that will have the replacement operations run against, explain clearly what parts you want replaced, and what will be replacing the old parts.
    Soldiers, from the height of these pyramids forty centuries look down upon you.
    ▪ Napoleon, on the eve of the Battle of the Pyramids
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    22
    Rep Power
    0
    Alright sorry for not being clear..basically what i want to preg replace is this line highlighted.
    Code:
    $parts = preg_split("/\.\(<font size="7">.*</font>\)\.\(<font size="7">.*</font>\)/", $file);
    I will like to replace the .* to either a person's IP address or Hostname for example 192.168.199.129 and SEAN. The first .* will be replaced with IP address and the second one will be replaced with Hostname..

    To make it simple, i will want to preg_replace a preg_split function as i am doing a search function..thanks




    Failed Preg Replace function that i did below..
    $batch_contents = preg_replace('~(preg_split\("/\\\\.\\\\(\.\*\\\\)\\\\.\\\\()([0-9.]+)(\\\\))~i', '${1}'.$newArgument, $batch_contents);
  6. #4
  7. Autodidact
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Location
    Canada
    Posts
    741
    Rep Power
    833
    Okay?

    What is the sequence of characters represented by $file? Tell me that, then tell me what strings in that sequence you want replaced with an IP address, and what strings in the sequence you want replaced with a hostname.

    Code:
    $batch_contents = preg_replace('~(preg_split\("/\\\\.\\\\(\.\*\\\\)\\\\.\\\\()([0-9.]+)(\\\\))~i', '${1}'.$newArgument, $batch_contents);
    That fails because the syntax is wrong.

    Give preg_replace a closer read.
    Soldiers, from the height of these pyramids forty centuries look down upon you.
    ▪ Napoleon, on the eve of the Battle of the Pyramids
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    22
    Rep Power
    0
    Alright basically i am doing a search function using regular expression.. this line $parts = preg_split("/\.\(.*\)\.\(.*\)/", $file); in the while loop is the line that i want to replace.. The first.* inside the bracket indicated by the orange colour font will be the hostname and the second .* will be the ip address. I will try to get the syntax right and post my improvements on it..thanks


    PHP Code:
    <?php


      
    //"$DOCUMENT_ROOT"."new/"; 
      
    $current_dir 'C:\xampp\htdocs\Audit_Reports';   //Put in second part, the directory - without a leading slash but with a trailing slash!
      
    $dir opendir($current_dir);        // Open the sucker
     

      
    echo ("<p><h1>List of Audit Reports:</h1></p><hr><br />");
      while (
    $file readdir($dir))            // while loop
      
    {

        
    $parts preg_split("/\.\(.*\)\.\(.*\)/"$file);
            if (
    is_array($parts) && count($parts) > 1) {    // does the dissected array have more than one part
            
    $extension reset($parts);        // set to we can see last file extension
            
    if ($extension == "Audit_Report" OR $extension == "audit_report")    // is extension ext or EXT ?
                 
    echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />";    // If so, echo it out else do nothing cos it's not what we want
            
    }
        }
      echo 
    "<hr><br />";
      
    closedir($dir);        // Close the directory after we are done

    ?>

    <form action="readdirectory.php" method='post'>Search Files:<p>
     Hostname: <input type="text" name="argument"<p>
    IP Address: <input type="text" name="argument1"<p>
    Date: <input type="text" name="argument1"<p>
    <p><input type="submit" name="submitFinal" value="Update"> &nbsp;<p>
    </form>
    </p>
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    22
    Rep Power
    0
    hmmm i still can't get it to work..

    This is the closest i can get to. I am trying to just edit the hostname at the moment.

    PHP Code:
    $batch_contents preg_replace("#preg_split\(\"\/\\\.\\\\\(\.\*\\\\\)#""/\.\\($newArgument\)"$batch_contents); 
  12. #7
  13. A94528C464D168DC82FE4933E9DF37
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Location
    California
    Posts
    119
    Rep Power
    73
    This will match both ".*"s using the string "$parts = preg_split("/\.\(.*\)\.\(.*\)/", $file);"

    Code:
    \$parts = preg_split\(\"\/\\\.\\\((\.\*)\\\)\\\.\\\((\.\*)\\\)\/\", \$file\);

IMN logo majestic logo threadwatch logo seochat tools logo