2

I need some PHP help with strings.

I have a textbox field where users will enter a facebook profile link.

Example: http://facebook.com/zuck

Now the problem is I need to have EXACTLY this string: "http://graph.facebook.com/zuck".

Inputs could be anything like:

http://facebook.com/zuck

http://www.facebook.com/zuck

www.facebook.com/zuck

facebook.com/zuck

What's the best way to do that? Thank you in advance.

3
  • A regular expression checking for facebook.com/username would be the best choice given the inputs you have listed. Commented Jun 1, 2013 at 15:15
  • How do you mean? Can you please elaborate? Commented Jun 1, 2013 at 15:21
  • I've added an answer with a regular expression. It's a fuzzy-match approach, in that blahblahfacebook.com/asdf/1234 would match "asdf" But I assume you're trying to be aggressive in your matching. Commented Jun 1, 2013 at 15:29

6 Answers 6

3

To accept anything in the format of facebook.com/username where username is alphanumeric with dots, dashes, and underscores (not sure what Facebook allows exactly):

if (preg_match('%facebook.com/([a-z0-9._-]+)%i', $input, $m))
{
  echo 'http://graph.facebook.com/', $m[1], "\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant response! Thank you so much for this. :) Worked!
2

Why don't you just ask the user for their username? Instead of accepting a wide variety of input, design the form so that they only have to put in their username.

Something along the lines of this;

enter image description here

This way, you don't even have to validate or store anything other than their username. This could be super helpful down the road when Facebook moves fast and breaks stuff, like the URLs of users. Or if you want to form URLs for something other than graph API, you won't have to pull apart an existing URL.

1 Comment

Well, you're right. But I would need to change the design and really can't be asked to do that. :/ Plus, some people don't know how to get their usernames :P
0

If given inputs will be always as the ones you give i think that strstr function would hadle this

$array = array('http://facebook.com/zuck', 'http://www.facebook.com/buck', 'www.facebook.com/luck', 'facebook.com/nuck');

foreach($array as $data)
{
    if(strstr($data, 'facebook.com/'))
    {
        echo 'http://graph.'.strstr($data, 'facebook.com/') . '<br>';
    }
}

This will output

http://graph.facebook.com/zuck
http://graph.facebook.com/buck
http://graph.facebook.com/luck
http://graph.facebook.com/nuck

Comments

0

Find the last slash in the input

$lastpos =  strrchr ( $input , '/' )

Manually concatenate the url and everything after that last slash.

$new_url = 'http://www.facebook.com' . substr($input, $lastpos);

Comments

0
$url = 'http://facebook.com/zuck';
$array = explode('/', str_replace('http://', '', $url));
$username = $array[1];
$finalurl = 'http://graph.facebook.com/zuck'.$username;

echo $finalurl;

This will work with any format of input URL.

Comments

0

Something along the lines of:

Pattern:

(https?://)?(www\.)?(.+?)\/([^/]+)

Replace with:

http://graph.$3/$4

Test it here:

http://www.regexe.com/

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.