0

A function in PHP returns me following js snippet in a string

<script type="text/javascript"> 
function gformRedirect()
{
  document.location.href='https://plamsn.web/fr/demande-de-pret?montant=400%24&email=jon%40doe.com';
}
</script>

I want to extract, href from the above string

https://plamsn.web/fr/demande-de-pret?montant=400%24&email=jon%40doe.com

I tried the following

$hrefStartIndex = strpos($redirect, 'http');
$hrefEndIndex = strpos($redirect, ';}');
$href = substr($redirect, $hrefStartIndex, $hrefEndIndex);

This seems to give some issues and i am not sure if this is how i should go with, any pointer on how to go about it will be appreciated.

Thanks,

1
  • for reference you stumble on to a JSONP request, maybe look more info that way? en.wikipedia.org/wiki/JSONP Commented Oct 18, 2015 at 5:41

4 Answers 4

1

try this:

$url = "<script type=''text/javascript'> function gformRedirect(){document.location.href='https://plamsn.web/fr/demande-de-pret?montant=400%24&email=jon%40doe.com';}</script>";

$first_step = explode( 'document.location.href=' , $url );
$second_step = explode(";}" , $first_step[1] );
echo $second_step[0];
Sign up to request clarification or add additional context in comments.

Comments

1

Please remember to change the start position, if there are any changes in the string.

<?php
$url='<script type="text/javascript"> function gformRedirect(){document.location.href="https://plamsn.web/fr/demande-de-pret?montant=400%24&email=jon%40doe.com";}</script>';

$url=substr($url,81,strlen($url));
$url=rtrim($url,'";}</script>');
echo $url;
?>

1 Comment

it returns ://plamsn.web/fr/demande-de-pret?montant=400%24&email=jon%40doe.com
1

solution using regex:

<?php
    $url='<script type="text/javascript"> function gformRedirect(){document.location.href="https://plamsn.web/fr/demande-de-pret?montant=400%24&email=jon%40doe.com";}</script>';
    preg_match("/http[^\"]*/", $url, $output_array);
    echo $output_array[0];
?>

Comments

0
add_filter( 'gform_confirmation', function ( $confirmation, $form, $entry, $ajax ) {
GFCommon::log_debug( __METHOD__ . '(): running.' );
if ( isset($form['confirmation']['type']) && isset($form['confirmation']['url']) && $form['confirmation']['type']="redirect" && $form['confirmation']['url']!="" && $form['id'] == 3) {
    $url          = esc_url_raw( $form['confirmation']['url'] );
    GFCommon::log_debug( __METHOD__ . '(): Redirect to URL: ' . $url );
    $confirmation = 'Thanks for contacting us! We will get in touch with you shortly.';
    $confirmation .= "<script>window.open('".$url."', '_blank');</script>";
}

return $confirmation;
}, 10, 4 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.