Jump to content

Drongo_III

Members
  • Posts

    579
  • Joined

  • Last visited

Posts posted by Drongo_III

  1.  

    I changed what you used slightly but this will match both your class and the content of the div

     

    
    
    preg_match_all("|<div class='(.*?)'>(.*?)</div>|",
        "<div class='mainful'>this is testing of the string n thats it </div>",
        $out, PREG_PATTERN_ORDER);
    echo "<pre>";
    print_r($out);
    

     

     

    I need to get the content of this div.

    but getting nothing

    $str = "<div class='mainful'>this is testing of the string n thats it </div>";
    $str = preg_quote($str, "/");
    $result = preg_match_all("/^<div class=\/'mainful\/'>(.*?)<\/div>$/", $str , $matches_results);
    if($result){
        print_r($matches_results); 
        exit;
    } else {
         echo "no match ";
    }
    

  2. Hi shadow

     

    This is quite hard to wrap your head around.

     

    The reason the order works like that can be shown with a normal variable.

     

    For instance:

     

    
    var1 = "TIM";
    var1 = "My name is $var1";
    echo var1;
    
    // Will  echo "My name is TIM"
    
    

     

    I believe the reason this happens is because before the system assigns the new variable name to the second $var1 it's saying - "right, what does the value of the second var1 need to be?" And in this case its seeing a string "MY name is" followed by a variable (the first instance of $var1). So it evaluates that part of the statement - i.e. The new value for $var1 should be "My name is Tim" and then it overwrites the value held for $var1.

     

    I think it's important to understand that is the way variables work and not to confuse it as a quirk of references

     

    At least that's my understanding of it...

     

     

    Thanks alot marcelobm for the reply

     

    why wouldnt it echo bob first before My name is?

    outputing Bob My name is

     

    why does $bar = &$foo go ahead of 'Bob'

  3. Hi shadow

     

    This is quite hard to wrap your head around.

     

    The reason the order works like that can be shown with a normal variable.

     

    For instance:

     

    
    var1 = "TIM";
    var1 = "My name is $var1";
    echo var1;
    
    // Will  echo "My name is TIM"
    
    

     

    I believe the reason this happens is because before the system assigns the new variable name to the second $var1 it's saying - "right, what does the value of the second var1 need to be?" And in this case its seeing a string "MY name is" followed by a variable (the first instance of $var1). So it evaluates that part of the statement - i.e. The new value for $var1 should be "My name is Tim" and then it overwrites the value held for $var1.

     

    At least that's my understanding of it...

     

     

    Thanks alot marcelobm for the reply

     

    why wouldnt it echo bob first before My name is?

    outputing Bob My name is

     

    why does $bar = &$foo go ahead of 'Bob'

  4. Hi mate

     

    Try this as the pattern:

     

    
    $patterns[0] = '/[^\w@.]/';
    

     

    essentially that pattern means replace everything except what is inside the square brackets.

     

    The \w applies too all letters, numbers and the underscore. If you also want to add in a hyphen as an allowed character then use:

     

    $patterns[0] = '/[^\w@.\-]/';

     

    Hope that helps matie :) I have had a good refresher in simple regex today hehe

     

     

  5. Better version

     

    
    
    
    $string = 'John_Smith   533r3r3 >>>';
    
    $patterns = array();
    $patterns[0] = '/[^\w]/';
    
    
    $replacements = array();
    $replacements[0] = '';
    
    echo preg_replace($patterns, $replacements, $string);
    
    
    
    
    

     

    This replaces everything except letters, numbers or underscores.

  6. Hmm

     

    I would try something like

     

    
    
    
    $string = 'John_Smith fee334343';
    
    // This is the pattern the pre_replace will use looking for matches
    $patterns = array();
    $patterns[0] = '/[ \-_]/';
    
    
    // The array value here that corresponds to the above pattern will replace matches 
    //with the contents of the array value. So in this case it replaces anything found
    //in the pattern with '' - i.e. removes spaces, hyphens etc.
    $replacements = array();
    $replacements[0] = '';
    
    echo preg_replace($patterns, $replacements, $string);
    
    
    

     

     

     

    Great, thanks, I thought it looked long just to do a simple check, but it worked so I was happy with that!

     

    Thanks again.

     

    Also, have you any idea how to remove everything from a string except letters, numbers and the @ sign,

     

    so:

     

    "hello123-123" = "hello123123"

    "John o'Leary" = "John oLeary"

    "aaaaaa11111111_1111@" = "aaaaaa111111111111@"

     

    Thanks again.

  7. Hi mate

     

    Err I was having a fiddle with this and a much more simpler version would be:

     

    <?php 
    
    $pattern5 = '/[\w]?[\d]+/';
    
    $email  = '111eeee';
    
    if (preg_match($pattern5, $email)) {
    
    echo "your pattern matched";
    
    
    }
    
    
    else {
    
    echo "Your patten didn't match";
    
    }
    
    ?>
    
    

     

    That is a much simpler way of matching alphanumeric characters or just numeric. It won't match just letters though. Sorry looked at regex sooo long ago that i thought it needed to be a lot more complicated than it did!

  8. Hi mate

     

    Ok this is testing my limited regex knowledge to the limits here but i think this works:

     

    <?php
    
    
    $pattern2 = '/^(?=.*\d)|(?=.*\d)(?=.*[a-zA-Z]).{1,}$/';
    
    $email  = 'uhuuyyv576576576';
    
    if (preg_match($pattern2, $email)) {
    
    echo "your pattern matched";
    
    }
    
    
    else {
    
    echo "Your patten didn;t match";
    
    }
    
    ?>
    
    

     

    It should match any pattern of letters and number, or just number but it won't match just letters...hehe confusing myself now...

  9. I think if you just want to match a single charcter you're over complicating it using regex.

     

    Try this:

     

    <?php 
    
    $email  = 'name@example.com';
    
    if (strstr($email, '@')) {
    
    echo "your pattern matched";
    
    }
    else {
    
    echo "Your patten didn't match";
    
    }
    
    ?>

  10. Ok.

     

    Well first thing is to fix that error.

     

    Have you tried removing the

     mysql_real_escape_string($question)); // THIS IS LINE 177

    from that line of code and seeing if it works then? That line is looking for a variable $question that hasn't been assigned yet (at least that's how the logic goes in my head). I would start by removing that and seeing what you get.

     

     

  11.  

    Well I think when its parsed the system essentially looks for an opening { and if no other { is found then the next } is considered the closing one. Whereas is two { { are found then it will look for the next }} to close. That make more sense hehe?

     

    Thanks for the reply

     

    The part that I dont understand or cant wrap my head around :) is that how does the script know which } to what statement it belongs too? Notice at the end of my code it has several }.

    what really confuses me the most is the two } above this line

     

    $_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user.

     

    Jedit is telling me those two } goes with the statements just above it but how does the code know  its not for the statements at the begining of the script that ends at the end of the script.

     

    This is really hard to explain on what is confusing me hope i explained it. I really need help understanding this cause its like the only thing left that really has me confused on PHP :(

  12.  

    Bit hard to say without seeing the code in it's entirety i'd say. But yeah if you have session start at the top and it's the first thing then that should be ok.

     

    First off i would ensure you're getting data from your query.

     

    So comment out your session bit and just try echoing your $row[0]  to make sure that bit is ok.

     

    Can you post more of the code and the function you start your session in?

     

     

    The function is in a separat file, and when i add session_start() in the very beginning, it gives me an error saying, that the session already is started, therefore, there is session_start() in the top.

    Is that wrong?

  13. Couple of things:

     

    1) did you start the session? Can;t see that in your code.

    2)Before you set the session variable have you tried just echoing the query results to make sure you're getting data? Sorry if i'm stating the obvious.

     

     

     

    Thank you very much, but it doesn't work.

     

    $query = "SELECT question FROM registertest ORDER BY RAND() LIMIT 1";
            $result = mysql_query($query) or die(mysql_error());
            
            if (!$result) {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $query;
                die($message);
            }
            
            $row = mysql_fetch_row($result);
            $_SESSION['registertestquestion'] = $row[0];
            return $row;

     

    The session part is because i need it later again.

    At this point, it returns nothing?

  14. Sorry the reason i asked my question is because I thought you had to say something like

     

     
    
    if ($count % 4 == 0) {
    
    // Do the new line
    }
    
    
    When I tried using just
    
    [code] 
    if($count % 4){
    // Do the new line
    }
    

     

     

    It didn't work for me and just echoes the new line constantly :/ So trying to figure out if I'm doing something wrong or misundestanding the way it works. Or whether the code should have ==0 for this sort of function.

     

    For this effect you use the modulus operator along with a counter

     

    Example, using part of your code

    $count = 0;
    while($tir = mysql_fetch_array($tiquery)) {
    $tiid = $tir['itemid'];
    $tin = $tir['name'];
    $tiim = $tir['image'];
    $tid = $tir['description'];
    
    echo "<img src=/images/items/$tiim>";
    if($count % 4) echo "<br />\n";
    
    $count++;
    }
    

  15. Sorry to jump in on this one. What does the modulus evaluate to in your if statement?

     

    Is it looking for a division by four that has no remainder and if it finds it the statement is true?

     

     

     

     

    For this effect you use the modulus operator along with a counter

     

    Example, using part of your code

    $count = 0;
    while($tir = mysql_fetch_array($tiquery)) {
    $tiid = $tir['itemid'];
    $tin = $tir['name'];
    $tiim = $tir['image'];
    $tid = $tir['description'];
    
    echo "<img src=/images/items/$tiim>";
    if($count % 4) echo "<br />\n";
    
    $count++;
    }
    

  16. Does this line in your code work?

     

     	
    
    $degree_id_query = "SELECT degree_id FROM degree WHERE degree_type ='".$degree_Array[$i]."'";
    

     

    Surely if you wrap a double quoted variable in single quotes it will be taken as a literal and not the value it represents?

     

     

     

     

    Gosh, i just figured it out. 

     

    $user_degree_insert_query = "INSERT INTO `user-degree`(

     

    user-degree didn't have the surrounding  ``.   

     

    Why is it that sometimes it seems i need these and sometimes not.

  17. The ^ character defines the start of the pattern and $ defines the end.

     

     preg_match("/^[a-zA-Z0-9]+$/") 

     

    Not sure if it makes a difference but i would always put the ^ outside of the square brackets.

     

    For your logic don't you want:

     

    
    If (pregmatch(blah blah))   //i.e. if the prematch satisfies what you want from the username grab query
    
    {
    grab database stuff
    }
    
    else {
    invalid user
    
    }
    

     

    Thanks alot for the help

    bare with me im a huge noob :)

     

    Ya i couldnt figure out why the ^ was in there i just removed that.

    I think it is working cause when I log in it says Invalid Username. So that means its working right i just have the double negitive in there?

     

    so to fix this I would need to put the else statement on the other side of $query-------------------

    is the reason its a double negitive is cause im now using preg_match instead of the ereg?

     

    Thanks

     

    <?php

    if(isset($_POST['Login'])) {

     

    if(!preg_match('[A-Za-z0-9]',$_POST['name'])){ // before we fetch anything from the database we want to see if the user name is in the correct format.

            echo "Invalid  Username.";

    }else{

     

    $query = "SELECT password,id,login_ip FROM users WHERE name='".mysql_real_escape_string($_POST['Username'])."'";

    $result = mysql_query($query) or die(mysql_error());

    $row = mysql_fetch_array($result); // Search the database and get the password, id, and login ip that belongs to the name in the username field.

  18. So besides my dubious logic inside the trim function (thanks for explaining that bit) it's ok to call a function from a while loop like this?

     

     

    array_walk_recursive is recursive for arrays. The function definition that you pass it will never receive an array as data, so you don't need the extra is_array logic inside the trim_all function.

  19. Thank Btellez

     

    Explored a bit more into headers after your suggestion and got it working along what you said

     

      
    header("Content-type: text/csv");
    		header("Content-Disposition: attachment; filename=file.csv");
    		header("Pragma: no-cache");
    		header("Expires: 0");
    
    		echo  $header . "\n" . $mydata;
    
    

     

     

    One thing I wanted to clarify.  Is it ok to called functions from within while loops.  For instance in my script I call a function from the while loop to trim the data - is that ok or do you think it's bad practice as it'll consume resources?

     

    Drongo

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.