SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match_all result = "Array"

    Hi all,

    Got a quick question for you this time.

    From the below preg_match_all() will always return a value of "Array" regardless what I try. To put this into plain code terms, $content will always = "Array".

    I have tested whether it's getting the right input and it is. There are definetly [test123] and [/test123] custom tags in the php file it's reading into $buffer which later gets passed into preg_match_all() as $current_line. It should spit out whatever it between these tags.

    The bug has got to be in the preg_match_all() right? Thanks in advance.

    Here's the code:


    function get_all_content_between($string, $start, $end)
    {
    preg_match_all( '/$start(.*)$end/s', $string, $match );
    return $match[1];
    }


    $rss_source_file = fopen("$rss_from_file", "r") or die("can't open file [SOURCE]");
    $rss_write_file = fopen("$rss_to_file", "a") or die("can't open file [DESTINATION]");


    while (!feof ($rss_source_file))
    {
    $buffer = fgets($rss_source_file);
    $lines[] = $buffer;
    }
    $array_count = count($lines);


    $matchtag_start = "[test123]";
    $matchtag_end = "[/test123]";
    for ($i = 0; $i < $array_count; $i++)
    {
    $current_line = $lines[$i];
    $content = get_all_content_between($current_line, '\(\$matchtag_start\(', "\)\$matchtag_end\)");

    print $content;

    fwrite($rss_write_file, $content ."\r\n");

    }

    fclose($rss_source_file);
    fclose($$rss_write_file);

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    preg_match_all returns an array of arrays.

    Use var_dump to see what's going on.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's because it returns an array. If you echo an array it will simply output "Array".

    Try using var_dump to see what is output, then model your code around that.

    Edit:

    Beaten to it! Good call.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys.

    Now it's printing "array(0) { }" which would suggest it's not even finding the [123test] whatever goes here [/123test] in $current_line

    I was thinking this was because [123test] and [/123test] might be on a separate line but I'm reading the file in as one line anyway. The input is good as if I just print the input as I read it then it will simply render the source page inside the current php file (where the script is run).

    It's got to be something with /$start(.*)$end/s in preg_match_all() as I can't picture it any other way.

  5. #5
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...or it could be the way I've passed '\(\$matchtag_start\(', "\)\$matchtag_end\)" in get_all_content_between().

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to make sure that the regex string you pass to preg_match_all is both valid, and what you want. There's lots of special characters in regular expressions. [ and ] are special. They define a character class. You can't just allow put anything you want into a regex and expect it to work.

    You could make use of preg_quote() to ensure the start and end strings are interpreted literally.

    You could also just not use regular expressions. You can easily do the same with a combination of strpos(), strlen(), and substr()

  7. #7
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You need to make sure that the regex string you pass to preg_match_all is both valid, and what you want. There's lots of special characters in regular expressions. [ and ] are special. They define a character class. You can't just allow put anything you want into a regex and expect it to work.

    You could make use of preg_quote() to ensure the start and end strings are interpreted literally.

    You could also just not use regular expressions. You can easily do the same with a combination of strpos(), strlen(), and substr()
    Thanks. So far I've taken your first comment onboard and tried the below, sadly this may have been a further bug however the main bug is still in there.

    $content = get_all_content_between($current_line, '\(\preg_quote($matchtag_start)\(', "\)\preg_quote($matchtag_end)\)");

    I'm aware about doing it via the strxxx functions but also have come to learn that they're a lot slower than preg_match_all() which is why I'm trying to stick to it if possible.

    I've done a search for examples online that also use [whatever here ] custom tags with preg_match_all() and if there is a difference between how I'm passing the strings and how they are then it's very minute else I like to believe my eyes would spot it by now.


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
  •