php - Preg_replace transform parameter inside function -


is possible make preg_replace parse variables inside function?

i looking transform [shorturl]full-url[/shorturl] clickable short url.

i want this:

    $code = array(       ...       '#\[shorturl\]((?:ftp|https?)://.*?)\[/shorturl\]#i' => '<a href="'.file_get_contents("http://...some_api?url=$1").'">$1</a>',       ...     )     $result = preg_replace(array_keys($code), array_values($code), $text); 

but don't works... api receive "$1" url rather url.

any thoughts?

this cannot work.

have in execution sequence of example: file_get_contents gets executed before preg_replace called.

but want result of regular expression part of function call. solution: preg_replace_callback. function calls code every time match found. example:

preg_replace_callback('#\[shorturl\]((?:ftp|https?)://.*?)\[/shorturl\]#i',      function($a) {       return '<a href="'.               file_get_contents('http://...some_api?url='.$a).               '">'.$a.'</a>';      }, $text  ); 

i didn't test it, give idea.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -