Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts

    Preg_match_all $matches problem

    For each match, the script should insert a new record into the messages field using the corresponding match as the `to` field. However, due to my lack of understanding of this parameter I can't get it to work. Using $match[0], leaves the new record's to field with a value of 'array', what I have posted below results in an error telling me the to field cannot be null.
    PHP Code:
        $umMatch preg_match_all("/@[a-z]{1,}\s/i"$post$matches); //User mention match
    if (isset($umMatch)) {
            for(
    $i 0$i <= $matches[$i][$i]; $i++) {
                
    $uMSQL "INSERT INTO `messages` (`to`, `from`, `message`, `sent`) VALUES (:to, :from, 'tagged you in a post', NOW())";
                
    $uMQ $con->prepare($uMSQL);
                
    $uMQ->bindParam(':from'$authorPDO::PARAM_STR);
                
    $uMQ->bindParam(':to'$matches[$i][$i], PDO::PARAM_STR);
                
    $uMQ->execute();
            }
        } 
    Last edited by elitis; 06-15-2013 at 03:56 PM.
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    With a little updating, I can get the code to almost work. Only problems now are the foreach loop only works with one username (the to field) instead of inserting as many records as usernames and the @ in the regex stays instead of being removed to leave just the username.
    PHP Code:
    if (preg_match_all("/[@]+[A-Za-z0-9-_]+/i"$post$matches)) {
            foreach(
    $matches as $key=>$value) {
                
    $uMSQL "INSERT INTO `messages` (`to`, `from`, `message`, `sent`) VALUES (:to, :from, 'tagged you in a post', NOW())";
                
    $uMQ $con->prepare($uMSQL);
                
    $uMQ->bindParam(':from'$authorPDO::PARAM_STR);
                
    $uMQ->bindParam(':to'$value[0], PDO::PARAM_STR);
                
    $uMQ->execute();
            }
        } 
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •