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"
// ]

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

the redirect_uri is not alway = http%3A%2F%2Flocalhost%3A8888 . I'm sorry. I updated my post.
My exact goal to replace 'redirect_uri=http%3A%2F%2F----anything---' with 'redirect_uri=http%3A%2F%2Flocalhost'
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?
http%3A%2F%2Flocalhost/foo , and It's a string. It's not a URL Param.
Check this out: stackoverflow.com/questions/176284/… might get you started, you'll need str_replace and a regex of some description I think
|
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

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.