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 4615818
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:53:39+00:00 2026-05-22T01:53:39+00:00

how can I modify my function to make use of <select name=extra2 id=extra2 class=select_smaller>

  • 0

how can I modify my function to make use of

<select name="extra2"  id="extra2" class="select_smaller">
    <option value="Algema">Algema</option>
    <option value="Barkas">Barkas</option>
.
.
.
</select>

that I have on my forms and then creates the array input of the function below to get it work ?
$options = array('Algema', 'Barkas', 'Cadillac', …);

If this is not possible or a big thing, we can avoid the “get from dropdown list and create me an array input” and
just use the dropdown list as is, to produce my desired output.

The result is correct, the code works great but what I want is to avoid the copy-paste around 200 different dropdown lists with about 10 options each. Instead I will use a program for mass text input to paste the code in the front and end of the lists.

function makeSelect($name, $options) {
    foreach ($options as &$option) {
        $selected = isset($_GET[$name]) && $_GET[$name] == $option;
        $option = sprintf('<option value="%1$s"%2$s>%1$s</option>',
                          htmlspecialchars($option),
                          $selected ? ' selected="selected"' : null);
    }

    return sprintf('<select name="%1$s" id="%1$s" class="select">%2$s</select>',
                   htmlspecialchars($name),
                   join($options));
}

$options = array('Algema', 'Barkas', 'Cadillac', …);
// instead of array I prefer to use here something $options=the dropdown list as is.
    echo makeSelect('car', $options);
  • 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-22T01:53:40+00:00Added an answer on May 22, 2026 at 1:53 am

    Use regular expressions, for example (assuming $list_html contains HTML in the form you have cited):

    $count = preg_match_all('/<option value=\"([a-zA-Z0-9]*)\"\>/', $list_html, $matches);
    if ($count) {
        // something has been found
        $found_values = $matches[1];
    } else {
        $found_values = array();
    }
    

    This has been tested. If you assign value in the following way:

    $list_html = '<select name="extra2"  id="extra2" class="select_smaller">'
        .'<option value="Algema">Algema</option>'
        .'<option value="Barkas">Barkas</option>'
        .'</select>';
    

    and do print_r($found_values), the result will be:

    Array ( [0] => Algema [1] => Barkas )
    

    And that means, you get proper values for each option in array. Assuming of course, that these values contain small letters, big letters or ciphers, but nothing more (otherwise the regular expression has to be adjusted to meet your needs).

    EDIT:

    For your convenience, the same thing in form of a function:

    /**
     * Get all values of 'option' tags in given HTML
     * @param string $list_html 
     * @return array values of option tags or empty array if none
     */
    function extractOptionValues($list_html) {
        $regex = '/<option value=\"([a-zA-Z0-9]*)\"\>/';
        $count = preg_match_all($regex, $list_html, $matches);
        if ($count) {
            $found_values = $matches[1];
        } else {
            $found_values = array();
        }
        return $found_values;
    }
    

    Now it should be ok to do it in the following way:

    $options = extractOptionValues($list_html); // $list_html contains select HTML
    

    EDIT 2:

    The same mechanism included within your function could look like that:

    /**
     * Return HTML of select field with one option selected, built based
     * on the list of options provided
     * @param mixed $options array of options or HTML of select form field
     * @return string HTML of the select field
     */
    function makeSelect($name, $options) {
    
        if (is_string($options)) {
            // assuming the options string given is HTML of select field
            $regex = '/<option value=\"([a-zA-Z0-9]*)\"\>/';
            $count = preg_match_all($regex, $options, $matches);
            if ($count) {
                $options = $matches[1];
            } else {
                $options = array();
            }
        }
    
        foreach ($options as &$option) {
            $selected = isset($_GET[$name]) && $_GET[$name] == $option;
            $option = sprintf('<option value="%1$s"%2$s>%1$s</option>',
                              htmlspecialchars($option),
                              $selected ? ' selected="selected"' : null);
        }
    
        return sprintf('<select name="%1$s" id="%1$s" class="select">%2$s</select>',
                       htmlspecialchars($name),
                       join($options));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that when you define a function in vim, you can use the
I know that when you define a function in vim, you can use the
I have a function I can't modify: function addToMe() { doStuff(); } Can I
I need to modify the lm (or eventually loess ) function so I can
In order to give functions the option to modify the vector I can't do
Can anybody confirm or deny that I can modify default styles provided by StyleCop
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
I'm trying to understand code that I bought so I can modify it. In
I've got a query that calls a function in its select clause. The function
I am working with XSLT 1.0 (so I can't use the replace() function), and

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.