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 11 of 11
  1. #1
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Finding Text in a File

    'I'm working on a script to open a txt file and load all the text after a particular line (wherever the word "BEGIN" is found). I've tried both array_search() and array_keys() (since file() returns the contents of a file as an array). I also tried converting the array to a string at each key and checking the string for the word, but that doesn't work either. Does anyone see the problem, or have any other suggestions?

    PHP Code:
    <?php
    $file_contents
    =file('functions/'.$_GET['funcname'].'.txt');
    /* Attempt to find BEGIN in file
        $line=array_keys($file_contents,'BEGIN');
        print_r($file_contents);
        $start_line=settype($line[0],'string');
        $start_line=$start_line+2;
        echo $start_line;
    */

    // Try converting to a string each time...
    $found_code=false;
    foreach (
    $file_contents as $lines => $file_contents){
        if (
    $found_code==false){
            
    $file_contentss=$file_contents;
            
    settype($file_contentss,'string');
            echo 
    $file_contentss."<br />\n";
            if (
    $file_contentss=='BEGIN'){
                
    $found_code=true;
                
    //$lines++;
            
    }
        }
        else {
            echo 
    htmlspecialchars($file_contents);
        }
    }
    ?>
    (Yeah, I took out array_search() and replaced it with array_keys().)

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    you can find out if text is in an array with in_array ( http://uk3.php.net/function.in-array )

  • #3
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    OK, but I know it's there. I just need to find out where.

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Use strstr along with file_get_contents so it's one string instead of an array

  • #5
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    I initially made it an array so I could add line breaks, but I'm putting it in a textarea, so I guess that doesn't matter.

    And mr e, is there a way to do what you're saying, but then just take the first text after the "BEGIN"?

  • #6
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Maybe that plus str_replace("BEGIN\n","",string); (assuming there's always only one line after "BEGIN"). But will the "\n" work?

  • #7
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    No, it doesn't. But adding line breaks directly to the PHP script does...

    Final code:
    PHP Code:
    <?php
    $cont
    =file_get_contents('functions/'.$_GET['funcname'].'.txt');
    $scriptt=strstr($cont,'BEGIN');
    $script=str_replace("BEGIN

    "
    ,"",$scriptt);
    echo 
    '<textarea cols="100" rows="20">';
    echo 
    "\n";
    echo 
    $script;
    echo 
    "\n";
    echo 
    '</textarea>';
    ?>
    Thanks!

    Now, for something slightly different, but on the same page:
    How can I return everything between two strings?
    For example, I have the whole file loaded into a string and I want to find text that's between two easily distinguishable "tags" (like "BEGIN text" and "END text").
    Last edited by thesmart1; 06-02-2007 at 12:39 AM.

  • #8
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    To convert new lines (\n) to breaks you can use nl2br

    You could do str_replace('BEGIN', '', $string), then trim() to trim the beginning and ending whitespace (including \n)

    For your last question, look into preg_match and regular expressions, here's an example
    PHP Code:
    $string 'BEGIN This is a string END';

    preg_match('/BEGIN (.*) END/'$string$matches);

    echo 
    $matches[1]; // Outputs This is a string 

  • #9
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    I tried the preg_match and all I got was an empty array.

    PHP Code:
    <?php
    $funcarr
    =$_POST['function'];
    foreach (
    $funcarr as $incr => $funcarr){
        
    $cont=file_get_contents('functions/'.$funcarr.'.txt');
        
    preg_match('/BEGIN DOCUMENTATION (.*) END DOCUMENTATION/'$cont$matches);
        echo 
    $matches[1]."<br /><br /><br /><br />";
    }
    ?>
    This is one of the files I'm trying to get information from:

    functions/Factorial.txt
    Code:
    ********************
    *
    *  Factorial
    *  By P. J.
    *
    ********************
    
    REQUIRES <>
    
    BEGIN DOCUMENTATION
    <strong>factorial (Factorial)</strong><br /><br />
    To use:<br />
    Call factorial(n,inside), where n is an integer, and inside is left blank.<br />
    For example:<br />
    factorial(5) returns 120<br /><br />
    The actual math function:<br />
    n!
    END DOCUMENTATION
    
    BEGIN SCRIPT
    
    function factorial(n,inside){
        var orign=n;
        opern=n;
        while (n>1){
            n--;
            opern*=n;
        }
        if (n=1){
            if (!inside){
                document.getElementById('mathfunc_factorial').innerHTML=opern;
            }
        }
    }

  • #10
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Now you see how much fun regular expressions can be

    What you want is this
    PHP Code:
    preg_match('/BEGIN DOCUMENTATION\s(.*)\sEND DOCUMENTATION/s'$string$matches); 
    To explain it, you're looking for BEGIN DOCUMENTATION, then your not looking for a space, but in your example a newline, so I used \s which matches any whitespace character, then you're looking for anything (though the . by itself won't work), then another whitespace character, then END DOCUMENTATION

    Then I have the s modifier on the end because by default . won't match newlines (which is why it wouldn't work just by itself), which we need it to

    http://us.php.net/manual/en/referenc...ern.syntax.php is a great reference for regex syntax

  • #11
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Thanks! It works perfectly now.

    (and I just gave you some good rep )


  •  

    Posting Permissions

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