Viewed   79 times

I'm looking at Webmonkey's PHP and MySql Tutorial, Lesson 2. I think it's a php literal. What does %s mean? It's inside the print_f() function in the while loops in at least the first couple of code blocks.

printf("<tr><td>%s %s</td><td>%s</td></tr>n", ...



with printf or sprintf characters preceded by the % sign are placeholders (or tokens). They will be replaced by a variable passed as an argument.


$str1 = 'best';
$str2 = 'world';

$say = sprintf('Tivie is the %s in the %s!', $str1, $str2);
echo $say;

This will output:

Tivie is the best in the world!

Note: There are more placeholders (%s for string, %d for dec number, etc...)


The order in which you pass the arguments counts. If you switch $str1 with $str2 as

$say = sprintf('Tivie is the %s in the %s!', $str2, $str1); 

it will print

"Tivie is the world in the best!"

You can, however, change the reading order of arguments like this:

$say = sprintf('Tivie is the %2$s in the %1$s!', $str2, $str1);

which will print the sentence correctly.

Also, keep in mind that PHP is a dynamic language and does not require (or support) explicit type definition. That means it juggles variable types as needed. In sprint it means that if you pass a "string" as argument for a number placeholder (%d), that string will be converted to a number (int, float...) which can have strange results. Here's an example:

$onevar = 2;
$anothervar = 'pocket';
$say = sprintf('I have %d chocolate(s) in my %d.', $onevar, $anothervar);
echo $say;

this will print

I have 2 chocolate(s) in my 0.

More reading at PHPdocs

Saturday, December 17, 2022

You can suppress warnings with libxml_use_internal_errors, while loading the document. Eg.:

$doc = new DomDocument();
$doc->loadHTML("<strong>This is an example of a <pseud-template>fake tag</pseud-template></strong>");

If, for some reason, you need access to the warnings, use libxml_get_errors

Thursday, September 8, 2022

It is hard to use DOMDocument in this specific case, since it wraps automatically text nodes with <p> tags (and add doctype, head, html). A way is to construct a pattern as a lexer using the (?(DEFINE)...) feature and named subpatterns:

$html = <<<EOD
Don't wrap me<p>Hello</p><div class="text">wrap me please!</div><span class="title">wrap me either!</span> Don't wrap me <h1>End</h1>

$pattern = <<<'EOD'
    (?<self>    < [^W_]++ [^>]* > )
    (?<comment> <!-- (?>[^-]++|-(?!->))* -->)
    (?<cdata>   Q<![CDATA[E (?>[^]]++|](?!]>))* ]]> )
    (?<text>    [^<]++ )
        < ([^W_]++) [^>]* >
        (?> g<text> | g<tag> | g<self> | g<comment> | g<cdata> )*
        </ g{-1} >
# main pattern
(?: g<tag> | g<self> | g<comment> | g<cdata> )+

$html = preg_replace($pattern, '<code>$0</code>', $html);

echo htmlspecialchars($html);

The (?(DEFINE)..) feature allows to put a definition section inside a regex pattern. This definition section and the named subpatterns inside don't match nothing, they are here to be used later in the main pattern.

(?<abcd> ...) defines a subpattern you can reuse later with g<abcd>. In the above pattern, subpatterns defined in this way are:

  • self: that describes a self-closing tag
  • comment: for html comments
  • cdata: for cdata
  • text: for text (all that is not a tag, a comment, or cdata)
  • tag: for html tags that are not self-closed

[^W_] is a trick to obtain w without the underscore. [^W]++ represents the tag name and is used too in the tag subpattern.
[^>]* means all that is not a > zero or more times.

(?>[^-]++|-(?!->))* describes all the possible content inside an html comment:

(?>          # open an atomic group
    [^-]++   # all that is not a literal -, one or more times (possessive)
  |          # OR
    -        # a literal -
    (?!->)   # not followed by -> (negative lookahead)
)*           # close and repeat the group zero or more times 

All characters between Q..E are seen as literal characters, special characters like [ don't need to be escaped. (This only a trick to make the pattern more readable).
The content allowed in CDATA is described in the same way than the content in html comments.

[^<]++ all characters until an opening angle bracket or the end of the string.

This is the most insteresting subpattern. Lines 1 and 3 are the opening and the closing tag. Note that, in line 1, the tag name is captured with a capturing group. In line 3, g{-1} refers to the content matched by the last defined capturing group ("-1" means "one on the left").
The line 2 describes the possible content between an opening and a closing tag. You can see that this description use not only subpatterns defined before but the current subpattern itself to allow nested tags.

Once all items have been set and the definition section closed, you can easily write the main pattern.

Monday, August 15, 2022

wmode is a parameter exclusive to <embed> tag referring to Flash movies. The default value is wmode=window.


When wmode=window, the Flash movie is not rendered in the page. It is instead displayed in a separate window than the browser content (as inspected with Spy++ or WinSpy++). This mode will have the best performance as the browser does not have to redraw a portion of the page on each frame. However, this mode prevents you from having content appear above or below the Flash movie.

  • Best Performance
  • Rendered in separate window
  • Opaque background
  • Doesn't allow content below
  • Doesn't allow content above


When wmode=opaque, the Flash movie is rendered as part of the page. No window is created for the movie. The movie will be rendered with the background color set during the publishing process and no content will be allowed behind. On each frame, content which appear above the movie will have to be redrawn by the browser, thus affecting performance.

  • Good Performance
  • Rendered as part of the page
  • Opaque background
  • Doesn't allow content below
  • Allows content above


When wmode=transparent, the Flash movie is rendered as part of the page. No window is created for the movie. The background color of the movie will be transparent. Thus, any non-opaque section of the movie will allow underlying content to display. On each frame, content which appear above and below the movie will have to be redrawn by the browser, thus greatly affecting performance.

  • Fair Performance
  • Rendered as part of the page
  • Transparent background
  • Allows content below
  • Allows content above

EDIT : Here are the answers to your additional questions...

Is wmode a FLASH only attribute?
Yes, wmode is only available in <embed> tags embedding a Flash movie.

What are the impacts on performance between the different values?
wmode=window will have the best performance as the Flash movie is rendered completely separately from the page itself. The browser does not have to refresh nor calculate the z-index position of content appearing over the Flash movie since the Flash movie in this mode is rendered in a completely separate window (as can be inspected with Spy++).

wmode=opaque and mode=transparent both follow very similar rendering paths. They are however slower than wmode=window because the browser has to check elements to see if they render above than the movie and render them on each frame. Note that wmode=transparent is slower than wmode=opaque since it has to also render underlying content as well as superposing content.

So, in order of performance...

FASTEST ----------------------------- SLOWEST

Wednesday, August 17, 2022

'#someDiv' is a CSS3CSS selector which is semantically equivalent to getElementById('someDiv'), in that it will select the element with ID 'someDiv'. So:



// bracket notation will return a DOM element

// leaving it out will return a jQuery object
Wednesday, November 2, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :