• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

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

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

0
CABRLU63
Asked:
CABRLU63
  • 3
  • 2
1 Solution
 
Cornelia YoderArtistCommented:
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.
0
 
soapergemCommented:
The strtolower() function is a built-in function since PHP 3. It just converts a string into lowercase.
www.php.net/strtolower

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:

<?php
quick_template::parse_template();
?>

...it 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!
0
 
soapergemCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CABRLU63Author Commented:
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.

0
 
Cornelia YoderArtistCommented:
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. :)))
0
 
soapergemCommented:
Thanks! =) I would appreciate that.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now