Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:29:15+00:00 2026-05-24T04:29:15+00:00

Im new to php and have programmed in other languages. Im trying to solve

  • 0

Im new to php and have programmed in other languages. Im trying to solve a certain programming situation: Basically I need to access strings stored inside an object. The internal data structure of the object is an associative array. The values are the strings Im trying to access.

This is the code Im using:

<?php

class OrderAuthenticator
{
    private $OrderObj;


     public function __construct($Order)
    {
         echo 'Contructed an instance of Order Authenticator<br/>';
        $this->OrderObj = $Order;
        echo 'Instantiated OrderContainer<br/>';
    }


public function authenticate_Drinks()
    {
        //echo __LINE__ ;
//4 number or characters including spaces between them
        $pattern_drinkName = '([0-9a-zA-Z\s]{1,75})';
//100 characters with spaces allowed between them
        $pattern_drinkCalories = '([0-9]{0,3})';
//100 characters with spaces allowed between them
        $pattern_drinkCategory = '([0-9A-Za-z\s]{1,50})';
//100 characters with spaces allowed between them
        $pattern_drinkDescription = '([0-9A-Za-z\s]{0,300})';
        $pattern_drinkPrice = '([0-9.]{1,6})';
//echo __LINE__ ;
        $DrinkContainer = $this->OrderObj->getDrinkContainer();
//echo __LINE__ ;
        foreach($DrinkContainer as $Drink)
        {
            //print_r($Drink);
            echo __LINE__ ;
            echo '<br/>';

         }

}
?>

This code produces the following output:

Array ( 
    [0] => Drink Object ( 
        [dataArray:private] => Array ( 
            [drink_name] => SimpleXMLElement Object ( [0] => Gin ) 
            [drink_cals] => SimpleXMLElement Object ( ) 
            [drink_Category] => SimpleXMLElement Object ( [0] => Alocholic ) 
            [drink_desc] => SimpleXMLElement Object ( ) 
            [drink_price] => SimpleXMLElement Object ( [0] => 4.00 ) 
        ) 
    ) 
) 

Now, what I need to do is take the string values out and I need to run a regular expression check on each of those. So I need to store each of these strings in a variable in some kind of a loop.

I had this code trying to do that within the above loop but it didnt work:

$drink_name = $Drink->getName();
echo 'drink name = '.$drink_name.'<br/>';
$drink_calories = $Drink->getCalories();
echo 'drink calories = '.$drink_calories.'<br/>';
$drink_category = $Drink->getCategory();
echo 'drink category = '.$drink_category.'<br/>';
$drink_Description = $Drink->getDescription();
echo 'drink Description = '.$drink_Description.'<br/>';
$Drink_Price = $Drink->getPrice();
echo 'drink Price = '.$Drink_Price.'<br/>';

    if(!preg_match($pattern_drinkName, $drink_name))
    {
        echo __LINE__ ;
        return 'Drink name'.$drink_name .' did not match<br/>';
    }
    else if(!preg_match($pattern_drinkCalories, $drink_calories))
    {
        echo __LINE__ ;
        return 'Drink calories'.$drink_calories .' did not match<br/>';
    }
    else if(!preg_match($pattern_drinkCategory, $drink_category))
    {
        echo __LINE__ ;
        return 'Drink category'.$drink_category .' did not match<br/>';
    }
    else if(!preg_match($pattern_drinkDescription, $drink_Description))
    {
        echo __LINE__ ;
        return 'Drink Description'.$drink_Description .' did not match<br/>';
    }
    else if(!preg_match($pattern_drinkPrice, $Drink_Price))
    {
        echo __LINE__ ;
        return 'Drink Price'.$Drink_Price .' did not match<br/>';
    }
    else 
    {
        echo __LINE__ ;
        return 'Merchant Location input is valid<br/>';
    }   

Here is the Drink class :

<?php
class Drink
{

    private $dataArray;// = array();


    public function __construct()
    {
        echo 'Entered constructor for Drink.php<br/>';
        $this->dataArray = array();
    }

    public function setName($drink_Name)
    {
        echo 'Added Drink Name to DrinkObj= '.$drink_Name. '<br/>';
        $this->dataArray["drink_name"] = $drink_Name;
    }
    public function getName()
    {
        echo 'Inside Drink name<br/>';
        return $this->dataArray["drink_name"];
    }

    public function setCalories($drink_Cals)
    {
        echo 'Added Drink Calories to DrinkObj= '.$drink_Cals. '<br/>';
        $this->dataArray["drink_cals"] = $drink_Cals;
    }
    public function getCalories()
    {
        return $this->dataArray["drink_cals"];
    }

    public function setCategory($drink_Category)
    {
        echo 'Added Drink Category to DrinkObj= '.$drink_Category. '<br/>';
        $this->dataArray["drink_Category"] = $drink_Category;
    }
    public function getCategory()
    {
        return $this->dataArray["drink_Category"];
    }

    public function setDescription($drink_Desc)
    {
        echo 'Added Drink Description to DrinkObj= '.$drink_Desc. '<br/>';
        $this->dataArray["drink_desc"] = $drink_Desc;
    }
    public function getDescription()
    {
        return $this->dataArray["drink_desc"];
    }

    public function setPrice($drink_Price)
    {
        echo 'Added Drink Price to DrinkObj= '.$drink_Price. '<br/>';
        $this->dataArray["drink_price"] = $drink_Price;
    }
    public function getPrice()
    {
        return $this->dataArray["drink_price"];
    }  
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:29:15+00:00Added an answer on May 24, 2026 at 4:29 am
    $patterns = array(
        'name' => '([0-9a-zA-Z\s]{1,75})',
        'calories' => '([0-9]{0,3})',
        'category' => '([0-9A-Za-z\s]{1,50})',
        'description' => '([0-9A-Za-z\s]{0,300})',
        'price' => '([0-9.]{1,6})'
    );
    
    $DrinkContainer = $this->OrderObj->getDrinkContainer();
    
    foreach($DrinkContainer as $Drinks)
    {
        foreach($Drinks as $DrinkObject)
        {
            $properties = array(
                'name' => $DrinkObject->getName(),
                'calories' => $DrinkObject->getCalories(),
                'category' => $DrinkObject->getCategory(),
                'description' => $DrinkObject->getDescription(),
                'price' => $DrinkObject->getPrice()
            );
    
            foreach($properties as $propname => $propvalue)
            {
                if(!preg_match($patterns[$propname], $propvalue))
                {
                    return "Drink $propname $propvalue did not match<br/>";
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to PHP and have been trying to get a PHP application
I'm new to PHP and have installed on Linux to boot (also a newbie).
Hi I am quite new to php but i have been following some tutorials
I am pretty new to php, but I am learning! I have a simple
I am new to PHP and trying to get the following code to work:
I'm painfully new to PHP, and was trying to set up phpBB on my
Situation: A PHP application with multiple installable modules creates a new table in database
Basically I am trying to have jQuery replace the button with the form and
I'm going to have to learn php soon, because I need it for a
I'm a PHP Programmer, and totally new to C#. I have downloaded the Html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.