PHP 5 Unleashed quick_template class what does strtolower('$1') mean

Posted on 2006-05-23
Last Modified: 2008-02-01
Language: PHP5
Book: PHP 5 Unleashed

Question: In the book, an example of a class called quick_template was written
In the class, there is a function called parse_template($subset = 'main')

The function uses recursion to replase content placed inside % signs in an xhtml file with content in an array with a key whoes value is the same as the content between the % signs.

The recursion line is: $content = preg_replace("/\%([A-Z]*)\%/e", "quick_template::parse_template(strtolower('$1'))", $content);

What I do not understand is strtolower('$1')

I'm not sure what value $subset will have when the function is called

Question by:CABRLU63
    LVL 27

    Accepted Solution

    strtolower() is a php function that changes uppercase letters to lower case.  Exactly how it is used in the function is unclear unless you post the function code.
    LVL 6

    Expert Comment

    The strtolower() function is a built-in function since PHP 3. It just converts a string into lowercase.

    The preg_replace() function is another built-in one to perform a regular expression search/replace. The particular one you listed searches for any number (including zero) of uppercase letters that are surrounded by percent signs. It then takes the letters (uppercase, remember) between the percent signs, converts them to lower case, and then runs the parse_template() function on that. I don't know if you know anything about regular expressions or not, but in any case, the lowercase letter "e" at the end of "/\%([A-Z]*)\%/e" is the key that allows you to execute PHP functions inside the replacement.

    The $subset = 'main' is just the default value for $subset. In other words, if you were to run this command:

    ?> would assume that you meant to type 'main'. But in your example above, this means that $subset becomes the lower-case version of each value it finds in between the percent signs.

    Hope that makes sense!
    LVL 6

    Expert Comment

    Oh, and the '$1', used in this context (inside a regular expression search/replace), means to substitute the first parenthesized subpattern. You kind of have to know about regex's to understand that, but basically it's just a bunch of letters that were between percent signs, in your example.

    Author Comment

    Thank you very much

    I know some of regular expressions. I did not know that the $1 meant to take the first subpart. I understand the class and function much better.

    LVL 27

    Expert Comment

    Thanks for the points, CABFLU63, but you should share them with soapergem too -- he's the one who understood what the $1 part was doing.  Maybe ask the forum mod to readjust them a little. :)))
    LVL 6

    Expert Comment

    Thanks! =) I would appreciate that.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now