Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help with this PHP/Regular Expression problem please

  • 22-04-2011 10:21pm
    #1
    Closed Accounts Posts: 4,001 ✭✭✭


    Hi all

    I really hope someone can help me with this. I've been trying to solve it for hours but I am not getting anywhere.

    I have the following text in a file:

    [PHP]
    <?php
    /***** BEGIN *****/
    debug = 2
    timelimit = 1500
    charset = UTF8
    categories =
    "9"=>"01"
    "2"=>"07"
    "36"=>"25"
    "23"=>"10"
    "30"=>"10,11"
    "13"=>"25"
    "6"=>"14"
    "34"=>"02"
    "15"=>"26"
    "31"=>"12"
    "03"=>"12"
    "33"=>"13"
    "22"=>"15"
    "14"=>"05"
    "12"=>"04"
    "35"=>"19"
    "38"=>"06"
    "10"=>"06"
    "7"=>"29"
    "20"=>"12"
    "29"=>"12"
    "11"=>"03"
    "4"=>"16"
    "5"=>"20"
    "24"=>"21"
    "1"=>"21"
    "17"=>"18"
    "28"=>"08,09"
    "25"=>"22"
    "18"=>"27"
    "27"=>"28"
    "16"=>"23"
    "32"=>"24"
    joblist = <td align=\"left\" width=\"8%\" valign=\"top\" class=\"Body-Textdate\">.*<\/td><td .*><a href=\"([^\"]+)\" class=\"Body-Textlarge2\">([^<]+)<\/a><\/td><td .*>.*<\/td><td .*>.*<\/td>
    /***** END *****/
    ?>
    [/PHP]

    Note the third last line is a regular expression...

    I read the contents of the file into a variable called $contents.

    I then want to use a regular expression to select all the text between the BEGIN/END comments, so I am using the following:

    [PHP]
    preg_match_all("/\/\*\*\*\*\* BEGIN \*\*\*\*\*\/\s*(.*)\s*\/\*\*\*\*\* END \*\*\*\*\*\//U",$contents,$found);
    [/PHP]

    This works fine if I test it using a website like http://www.regexpal.com, but for some reason it won't work in my code. If remove the third last line (the regular expression) it works fine, so obviously this is the troublesome line.

    Does anyone know a way I can successfully select everything between the comments?

    Thanks for reading.


Comments

  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Works for me, although I added the 's' switch at the end for multi-line reading.
    <?php 
    $handle = fopen('wtf.php', 'r');
    $contents = fread($handle, filesize('wtf.php'));
    preg_match_all("/\/\*\*\*\*\* BEGIN \*\*\*\*\*\/\s*(.*)\s*\/\*\*\*\*\* END \*\*\*\*\*\//Us",$contents,$found);
    echo '<pre>'.htmlspecialchars($found[1][0]).'</pre>';
    ?>
    


  • Closed Accounts Posts: 4,001 ✭✭✭Mr. Loverman


    Thanks, I wasn't aware of the 's' switch. That does the trick!


Advertisement