PHP program which extracts attribute lists from custom tag PHP program which extracts attribute list from the custom tag. It also strips out the single and double quote if it exists around attribute value. 

&lt;?php

function parseAnswerTag($tag)
{
    // input =&gt; &lt;answer scale="10" points="7"&gt;
    // ouput =&gt; ["scale"=&gt;10, "points"=&gt;7]
    // get the answer tag and parse out the attributes of answer tag
    $returningObj = array();
    //get the splitted string as an array
    $arr = splitByDelimiter(array('delimiter' =&gt; " ", 'string'=&gt; $tag));
    // sliced the array to just have attributes otherwise will have answer element too
    foreach (array_slice($arr, 1) as $key =&gt; $value)
    {
        $arr[$key] = splitByDelimiter(array('delimiter' =&gt; "=", 'string'=&gt; $value)); 
        if(in_array('scale', $arr[$key]) or in_array('points', $arr[$key]))
        {
            // check to see if single or double quote exist in value of key
            if(preg_match("/['\"]/",$arr[$key][1]))
            {
                // makes a call to strip out the quotes
                $returningObj[$arr[$key][0]] = stripQuotes($arr[$key][1]); 
            }
        }
    }
    // returns the associate key of attribute in the tag
    return $returningObj;
}

function stripQuotes($str)
{
    // input =&gt; "10"
    // output =&gt; 10
    // gets attribute with value and returns unquote string
    var_dump(strpos($str, "'") === FALSE);
    $start_point = 0;
    $end_point = 0;
    if(strpos($str, "'") !== FALSE)
    {
        $start_point = strpos($str, "'");
        $end_point = strpos($str, "'", $start_point+1);
    }
    elseif (strpos($str, '"') !== FALSE) 
    {
        $start_point = strpos($str, '"');
        $end_point = strpos($str, '"', $start_point+1);
    }
    return substr($str, $start_point+1, $end_point - ($start_point+1));
} 

function splitByDelimiter($input)
{
    //gets a associate array with delimiter and string {text value to be splitted}, split them by passed delimiter and return them
    // $input['delimiter'] =&gt; value splitted by and $input['string'] =&gt; value to be splitted
    return explode($input['delimiter'], $input['string']);
}


Can you please give me some constructive suggestions so that I can improve?
Here are some things I noticed:


The function splitByDelimiter() is no more than a wrapper around explode(). Why not use explode() directly?
The function stripQuotes($str) can be a lot shorter: trim($str," '\"");. Note: You should never use ' or " inside attributes, use their HTML equivalents.
Inside parseAnswerTag() you use a foreach loop. Inside it you assign values back into $arr, but under the wrong keys because array_slice() will have generated new numeric keys.
I don't understand why you select for scale and point?


I'm sorry but I cannot help you with other details, because I'm unsure about the origin of the HTML, and what you exactly are trying to accomplish. I did make a shorter version of your function:

function parseAnswerTag($tag)
{
    $inside = trim($tag,'&lt;&gt; ');
    $input  = explode(' ',$inside);
    array_shift($input);
    foreach ($input as $attr)
    {
        $parts = explode('=',$attr);
        $name  = trim($parts[0]);
        $value = trim($parts[1]," '\"");
        $output[$name] = $value;
    }
    return $output;
}

echo '&lt;pre&gt;';
print_r(parseAnswerTag('&lt;answer scale="10" points="7"&gt;'));
echo '&lt;/pre&gt;';


So trimming can be very useful.

Keep in mind: Algorithms like these are almost always slightly flawed. A simple thing like a double space could trip it up. It's very hard to get it completely right. Especially if you make it complicated or use regular expressions.

I also have to point out that PHP has a build in DOM to deal with HTML code. You can load HTML into it, and manipulate it there. See:

http://php.net/manual/en/class.domdocument.php

In more complex cases this is the preferred way to deal with HTML.