SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking SOLVED :: Pass encoded stuff though a array with preg match

    Hi

    Im trying to pass encoded stuff though a array, that then goes to preg match(should had been decoded somewere on the way) to see if there is a match or not. I have simple examples below, im only a beginer and i hope you can understand what im trying to do

    This code is fine, it is what i need to add the decoding to. With info what it does below
    Code:
    $lines= array();
    
    // All this does is get word list that is encoded(pre encoded eg czo3OiJlbmNvZGVkIjs=, ready to be only decoded)
    $lines=file('myfile.txt') or problem('<b>System error code 2.</b>');
    
    foreach($lines as $word){ 
    //grab the footer to be compaired with our encoded word list that should had been decoded by now or it will try and match encoded stuff that dont exist
    $html = @file_get_contents('footer.php') or problem('<b>System error code 3.</b>');
    $html = strtolower($html);
    if (preg_match("/".trim($word)."/i", "$html") == 1) {  
    
    //if all is well the header well go and load the page
    require_once('images/header.php'); } 
    
    //else if we get to here words were not matched and we are not going to display the page at all and go to error page
    else { problem($settings['sys-variables']); } }
    
    // START problem()
    function problem($myproblem) {
    //html or what ever here
    echo($myproblem);
    exit();
    }
    myfile.txt
    Code:
    czoyODoiaWYgeW91IHJlYWRpbmcgbWUgaW0gZGVjb2RlZCI7
    czoxNToic29tZSBtb3JlIHdvcmRzIjs=
    czoyMDoiYW5kIG1vcmUgd29yZHMgYWdhaW4iOw==

    Simple decode
    $str = 'czoyODoiaWYgeW91IHJlYWRpbmcgbWUgaW0gZGVjb2RlZCI7';
    $decoded = unserialize(base64_decode($str));
    echo $decoded;

    I have tried so many way, it cannot be passed here like my example below to be decoded, pregmatch tries to match the encoded lines


    $encodedarraydata=file('myfile.txt');
    $lines = unserialize(base64_decode($encodedarraydata));
    $lines = array();

    I been though so many different ways without doing any good but i cant code complex stuff and hoping some one would be kind enough to help
    Last edited by luik; Jun 8, 2007 at 17:21.

  2. #2
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not completely sure on what you're after here... but you say in your foreach statement that the encoded information should've been decoded by then.

    I don't see anywhere this is would've happened.

    FYI: When posting code in the forums, use [ PHP ] instead of [ code ] - it color codes it better for easy reading

    If you want to see what an array is holding (such as $lines) you can use code like this:

    PHP Code:
    echo "<pre>";
    print_r($lines);
    echo 
    "</pre>"
    If you put this just above your foreach statement you will be able to see whether its been decoded yet or not
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  3. #3
    SitePoint Addict
    Join Date
    Sep 2005
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are reading your footer file with every loop of the array. You probably only need to read your footer file once.

    Is each line of your file myfile.txt encoded separately or does the entire content of the file include an encoded and serialized array?

    If each line is encoded then you must do the following:
    foreach($lines as $word){
    $word= unserialize(base64_decode($word));
    ...

    From your example, is each line a word or are there multiple words per line? You are reading the file and processing the lines as though each line in the file is a word with no other words (which is fine).
    PHP Shopping Cart Software Easy Ecommerce Shopping Cart Script.
    PHP Super Cart is 100% template driven.

  4. #4
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi luik,

    Just a few things you may want to look at here and they all relate to the foreach loop:

    1. Functions can only be declared once so they should not be declared inside a loop. You can declare the 'problem' function before the loop.

    2. You need to read the 'footer.php' file in the $html variable once, so you can do that also before the loop starts.

    3. You can easily decode your lines within the foreach loop.

    PHP Code:
    // START problem()
    function problem($myproblem) {
        
    //html or what ever here
        
    echo($myproblem);
        exit();
    }

    //grab the footer to be compaired with our encoded word list that should had been decoded by now or it will try and match encoded stuff that dont exist
    $html = @file_get_contents('footer.php') or problem('<b>System error code 3.</b>');
    $html strtolower($html);

    $lines= array();
    // All this does is get word list that is encoded(pre encoded eg czo3OiJlbmNvZGVkIjs=, ready to be only decoded)
    $lines=file('./myfile.txt') or problem('<b>System error code 2.</b>');

    // We'll use a variable to test for a match.
    $wordMatch false;
    foreach(
    $lines as $word){
        
    // decode the string here
        
    $word unserialize(base64_decode(trim($word)));
        if (
    preg_match("/".$word."/i""$html") == 1) {
            
    $wordMatch true;
            break; 
    // no need to continue this loop
        
    }
    }

    if (
    $wordMatch) {
        
    //if all is well the header well go and load the page
        
    require_once('images/header.php');
    } else {
        
    problem($settings['sys-variables']);

    Last edited by Trent Reimer; Jun 8, 2007 at 10:35. Reason: forgot to include the 'myfile.txt' file

  5. #5
    SitePoint Member
    Join Date
    May 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Jeanco, that does the trick

    And thank you to Trent Reimer, your info is on summing up my code is excellent, i never new those things by being self taught, i might have to reveiw some of my other code and sort it out, interesting how it can work but not nesicarily mean it has been done correctly. It sounds like html validation, stuff can work but does not mean its spot on.


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
  •