Jump to content

Recommended Posts

Hi,

 

Any help with this would be much appreciated, I have racked my brains but to no avail. I am new to all this (3 weeks experience).

 

I am working through a book at the mo and this code popped up

 

function tagwrap($tag, $txt, $func = " ") {

        if ((!empty($txt)) && (function_exists($func))) {

        $txt = $func($txt);

        return "<$tag>$txt</$tag>\n";

}

}

 

I am happy with what it does but not how it does it.

what does this do?

$txt = $func($txt);

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/189892-noob-help-plz/
Share on other sites

hello again, I dont really understand why people are viewing this and not responding. I know it is a silly question.

 

  
  8: function tagWrap( $tag, $txt, $func="" ) {
  9:     if ( ! empty( $txt ) && function_exists( $func ) ) {
10:         $txt = $func( $txt );
11:         return "<$tag>$txt</$tag>\n";
12:     }
13:  }
14: 
15: function underline( $txt ) {
16:     return "<u>$txt</u>";
17: }
18: 
19: print tagWrap('b', 'make me bold'); 
20: // <b>make me bold</b> 
21: 
22: print tagWrap('i', 'underline me too', "underline");
23: // <i><u>underline me too</u></i>
24: 
25: print tagWrap('i', 'make me italic and quote me',
26:     create_function('$txt', 'return ""$txt"";'));
27: // <i>"make me italic and quote me"</i>
28: 

 

I have found the code online and here it is above!

 

my query again, what does line 10 do?

 

I cant bring myself to skip this and carry on. I have been stuck on this one line for 72 hours!

Link to comment
https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002395
Share on other sites

I dont really understand why people are viewing this and not responding.

 

Because the forum section where you posted this (PHP Applications, Frameworks and Libraries »

Third Party PHP Scripts) has nothing to do with the problem. Moving thread to the PHP Help forum section...

Link to comment
https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002405
Share on other sites

It simply puts $txt into a function (although I'm not sure that function is) and the returned value of that is then placed back in to the $txt variable.

 

The function tagWrap has 3 parameters. Tag, Text and Func. Whatever function is passed into the tagWrap will depend on what it returns (will be nothing if $func refers to a non existant function).

 

If you look at line 15 you will see of the functions 'underline'. It's more or less an expanded function of that, but allowing it to be more dynamic (ie can use any function that exists within it, rather than just underline).

Link to comment
https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002427
Share on other sites

Nothing.

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

 

look at the first line of code. it uses the vars inside of (vars) as passed with the call to it.

$a=1
$b=2
$answer = add($a,$b)
function add($a,$b){
$sum = $a+$b;
return "$sum";
}

 

$answer would have a value of 3

 

Now look at the second line of code. $func is empty as passed to tagWrap, so how can function_exist find a non-named function that obviouslt does not exist? The statement is FALSE and does nothing. It is for you to put a function there for the function to use.

 

Confused?

http://www.php.net/manual/en/functions.user-defined.php

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002429
Share on other sites

The idea of the $func in the call is for you tp specify a function that can be applied to the text before it is wrapped

 

eg you may want to call strtoupper() on the text to make it upper case

In which case you simply call tagWrap('b', 'make me bold','strtoupper');

 

Then of course the if(function_exists()) checks the function you have supplied exists, and if so, calls it

Link to comment
https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002437
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.