Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Help with this PHP/Regular Expression problem please

  • 22-04-2011 11: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,144 ✭✭✭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