0

I have a Facebook link string:

"https://www.facebook.com/v2.5/dialog/oauth?client_id=*****&state=*****&response_type=code&sdk=php-sdk-5.1.2&redirect_uri=http%3A%2F%2Flocalhost%3A8888%2Ffacebook%2Flink&scope=email%2Cuser_birthday%2Cuser_photos"

I want to replace:

redirect_uri=http%3A%2F%2Flocalhost%3A8888

to

redirect_uri=http%3A%2F%2Flocalhost


I would do something like this if it's the browser URL, but it is a string inside a anchor tag.

if (isset($_GET['redirect_uri'])) {
    echo $_GET['redirect_uri'];
}else{
    // Fallback behaviour goes here
}

How do I do something like that?


I've tried

$permissions = 'user_birthday,user_photos';
$login_url = $fb->getLoginUrl(['email','scope'=>$permissions]);
$urls = explode("&", $login_url);
$redirect_uri = explode("=", $urls[4]);
$link = explode("%2F", $redirect_uri[1]);

dd($link);

// array:5 [▼
//   0 => "http%3A"
//   1 => ""
//   2 => "localhost%3A8888" // I want to replace this string with 'localhost'
//   3 => "facebook"
//   4 => "link"
// ]
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
code-8
  • 54,650
  • 106
  • 352
  • 604

3 Answers3

2

Maybe some regex replacement?

$url = 'https://www.facebook.com/v2.5/dialog/oauth?client_id=*****&state=*****&response_type=code&sdk=php-sdk-5.1.2&redirect_uri=http%3A%2F%2Flocalhost%3A8888%2Ffacebook%2Flink&scope=email%2Cuser_birthday%2Cuser_photos';

$pattern = '/redirect_uri=http\%3A\%2F\%2F[\%0-9A-Za-z]+facebook/';
$replace = 'redirect_uri=http%3A%2F%2Flocalhost%2Ffacebook';
$newURl = preg_replace($pattern, $replace, $url);
marcellorvalle
  • 1,631
  • 3
  • 17
  • 30
1

If your string is actually redirect_uri=http%3A%2F%2Flocalhost%3A8888 then do this:

$url = $_GET['redirect_uri'];
$url = str_replace('redirect_uri=http%3A%2F%2Flocalhost%3A8888', 'redirect_uri=http%3A%2F%2Flocalhost', $url);

This will search the string and make the required changes, then save it back to the $url variable.

Obviously, you could use $_GET['redirect_uri'], either way you should sanitise the input:

Filter Input

Filters - Sanitise

See also: http://php.net/manual/en/function.str-replace.php

Adam
  • 1,294
  • 11
  • 24
  • the `redirect_uri` is not alway `= http%3A%2F%2Flocalhost%3A8888` . I'm sorry. I updated my post. – code-8 Mar 29 '16 at 19:34
  • My exact **goal** to replace `'redirect_uri=http%3A%2F%2F----anything---'` with `'redirect_uri=http%3A%2F%2Flocalhost'` – code-8 Mar 29 '16 at 19:35
  • So if the domain URI was `http%3A%2F%2Fdomain.com/foo` you'd want it to be replaced with `http%3A%2F%2Flocalhost/foo`? – Adam Mar 29 '16 at 19:40
  • `http%3A%2F%2Flocalhost/foo` , and It's a string. It's not a URL Param. – code-8 Mar 29 '16 at 19:44
  • Check this out: http://stackoverflow.com/questions/176284/how-do-you-strip-out-the-domain-name-from-a-url-in-php might get you started, you'll need str_replace and a regex of some description I think – Adam Mar 29 '16 at 19:53
  • marcellorvalle nailed it for you I think! – Adam Mar 29 '16 at 19:53
1

Considering this input string:

https://www.facebook.com/v2.5/dialog/oauth?client_id=*****&state=*****&response_type=code&sdk=php-sdk-5.1.2&redirect_uri=http%3A%2F%2Flocalhost%3A8888%2Ffacebook%2Flink&scope=email%2Cuser_birthday%2Cuser_photos

You can use this flexible regex pattern with an empty replacement string:

/&redirect_uri=\S+?localhost\K%[^%]+/

Replacement Demo Link

This pattern (in plain terms) says:

  • match "&redirect_uri="
  • match any non-white-character one or more times (stopping as soon as possible)
  • match "localhost"
  • restart the "full string match" (\K)
  • match "%"
  • match one or more non-% characters
mickmackusa
  • 43,625
  • 12
  • 83
  • 136