Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a function inside string?

Tags:

string

php

'<a rel="nofollow" href="$1" class="bbc_link new_win" target="_blank">'

I would like use the urlencode() function:

 '<a rel="nofollow" href="urlencode($1)" class="bbc_link new_win" target="_blank">'

... but I can't use this:

 '<a rel="nofollow" href="'.urlencode($1).'" class="bbc_link new_win" target="_blank">'

... because $1 is not a variable in the string; it is instead a meta-variable in a simple free forum.

it send http://www.test.com/out.php?out=http://www.example.com

like image 693
monkey_boys Avatar asked Dec 15 '10 06:12

monkey_boys


People also ask

How do you call a function inside a string?

There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.

Can you use a string to call a function?

In summary, to call a function from a string, the functions getattr() , locals() , and globals() are used. getattr() will require you to know what object or module the function is located in, while locals() and globals() will locate the function within its own scope.

What is string function with example?

The most basic example of a string function is the length(string) function. This function returns the length of a string literal. e.g. length("hello world") would return 11. Other languages may have string functions with similar or exactly the same syntax or parameters or outcomes.

How do you call a function from a string in PHP?

To call a function from a string stored in a variable, use $func.


1 Answers

how about this crazy hack?

<?
$_ = 'urlencode';
echo "<a rel=\"nofollow\" href=\"{$_($1)}\" class=\"bbc_link new_win\" target=\"_blank\">";
like image 171
Oliver O'Neill Avatar answered Sep 23 '22 03:09

Oliver O'Neill