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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:05:10+00:00 2026-05-16T00:05:10+00:00

shopp(‘product’,’quantity’,’input=menu&return=true’); The above function from my CMS returns the value <select name=products[1][quantity] id=quantity-1><option selected=selected

  • 0
shopp('product','quantity','input=menu&return=true');

The above function from my CMS returns the value <select name="products[1][quantity]" id="quantity-1"><option selected="selected" value="1">1</option><option value="2">2</option><option value="3">3</option></select>.

Any idea how I can get the number in that last option tag and assign it to a variable in php? In the above case, I’d like to assign the number ‘3’ to a variable, say, $aaa.

(BTW, the number of option tags is not fixed, and can go up to a few hundred.)

  • 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-16T00:05:11+00:00Added an answer on May 16, 2026 at 12:05 am

    Update:

    Oh boy, sometimes it is even easier than one thinks. There is already a function that counts substrings: substr_count().

    So now it is just:

    $number = substr_count($str, '</option>');
    

    So if you say it could be a few hundred option tags, this solution is much better as it does not generate intermediate arrays.


    Old answer: (is doing the same in a more complicated way)

    Here is another hacky way:

    The HTML string represents a select box that lets you select quantities. Thus, we can assume that the number of option tags represent the maximum number to choose from (in your example 3).

    So we only have to count the number of option tags. We can do this using a combination of str_word_count() and array_count_values().

    So assuming we have this HTML string (from your example):

    $str = '<select name="products[1][quantity]" id="quantity-1"><option selected="selected" value="1">1</option><option value="2">2</option><option value="3">3</option></select>';
    

    we can get the words be using str_word_count(). As there are opening and closing option tags, we are looking for closing tags for simplicity. Hence we have to tell the function to treat / also as word character:

    str_word_count($str, 1, '/');
    

    gives:

    Array
    (
        [0] => select
        [1] => name
        [2] => products
        [3] => quantity
        [4] => id
        [5] => quantity-
        [6] => option
        [7] => selected
        [8] => selected
        [9] => value
        [10] => /option
        [11] => option
        [12] => value
        [13] => /option
        [14] => option
        [15] => value
        [16] => /option
        [17] => /select
    )
    

    As we can see /option occurs 3 times.

    Now we use array_count_values() to count them:

    array_count_values(str_word_count($str, 1, '/'));
    

    gives:

    Array
    (
        [select] => 1
        [name] => 1
        [products] => 1
        [quantity] => 1
        [id] => 1
        [quantity-] => 1
        [option] => 3
        [selected] => 2
        [value] => 3
        [/option] => 3
        [/select] => 1
    )
    

    So we can get the number of option tags and therefore the highest number by simply applying:

    $counter = array_count_values(str_word_count($str, 1, '/'));
    $number = $counter['/option'];
    

    Of course using a HTML parser would be better, but if you know that the function will always generate the HTML this way, counting the option tags should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.