-3

Hello everyone i got stuck in a scenario where i need to run a javascript function in php and get the output in php variable. Below is my code -

$test = '<script type="text/javascript">
    var sproutencodeemailRot13String = "<n uers=\"znvygb:qneeva@npbegvagreangvbany.pbz\" gnetrg=\"_oynax\">qneeva@npbegvagreangvbany.pbz</n>";      
    var sproutencodeemailRot13 = sproutencodeemailRot13String.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);
    }); 

    alert(sproutencodeemailRot13);
    </script>';

When i run above code i got the data in alert CLICK HERE FOR OUTPUT But when i removed the alert i am not getting any thing in the $test variable when i print this. Please tell me any one what i am doing wrong? I want to save this variable data in database.

  • 3
    You cannot "run a javascript function in php". They run in completely different environments. PHP runs on your server and Javascript runs in the visitors' browsers. By the time the Javascript executes, the PHP is no longer running. – Patrick Q Nov 06 '17 at 13:18
  • 1
    Read first comment. Either make your JavaScript function a php function, or use ajax to send data to backend. Do some tutorials first. – IamSilviu Nov 06 '17 at 13:20
  • Thanks @Patrik Q - So there is no way to get this javascript function output in php – Salman Ahmad Nov 06 '17 at 13:20
  • Client side script can not be called inside sever side script. – Sandip Nov 06 '17 at 13:26

1 Answers1

1

There's no way to run a Javascript function in PHP, but fortunately this is just a simple rot13 transformation, which PHP provides a native function for: str_rot13.

$str = "<n uers=\"znvygb:qneeva@npbegvagreangvbany.pbz\" gnetrg=\"_oynax\">qneeva@npbegvagreangvbany.pbz</n>";

echo str_rot13($str);

See https://eval.in/893933

iainn
  • 16,826
  • 9
  • 33
  • 40