2014. szeptember 12., péntek

PHP szövegformázó függvények

Az alábbi példában paraméterként adhatók meg a formázó függvények nevei is. Vastagbetűs, kicsinyített és dőlt betűs.

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 6.16</title>
</head>
<body>
<div>
<?php

function tagWrap( $tag, $txt, $func="" ) {
    if ( function_exists( $func ) )
        $txt = $func( $txt );
    return "<$tag>$txt</$tag>\n";
}

function subscript( $txt ) {
    return "<sub>$txt</sub>";
}

print tagWrap('b', 'make me bold');
// <b>make me bold</b>

print tagWrap('i', 'shrink me too', "subscript");
// <i><sub>shrink me too</sub></i>

print tagWrap('i', 'make me italic and quote me',
    create_function('$txt', 'return "&quot;$txt&quot;";'));
// <i>&quot;make me italic and quote me&quot;</i>

?>
</div>
</body>
</html>

Eredmény:

make me bold shrink me too "make me italic and quote me"

Nincsenek megjegyzések:

Megjegyzés küldése