1

I have a really simple question that I couldn't solve using online help...

Well I have an URL, eg. "www.google.com/hello/xxxxxxxx"

What I'm trying to do is get that xxxxxxx and print it. I have already gathered the URL of the page I want to split with a SQL statement, and it is now stored in the $row['url']. I have tried to get it it split when it reaches an "/"

This is my code right now:

$url = preg_split("[/.-]", $row['url']);
print_r($url);

But it doesn't work? Right now it doesn't even print anything... and I don't know what else to try!

2
  • 1
    Why? php.net/parse_url already does all this for you You're using preg_split wrong - there's no delimiters. e.g. '/[\/.-]/ Commented Jan 11, 2014 at 20:10
  • @MarcB Yes I know but I will need this for different stuff that doesn't involved URLs forcedly and I would like to use it as reference. What should I use instead of preg_split? Commented Jan 11, 2014 at 20:11

2 Answers 2

1

Is this what you are looking for ?

$url = "www.google.com/hello/xxxxxxxx" ;

echo substr( strrchr( $url, '/' ), 1 );

Output: xxxxxxxx

Sign up to request clarification or add additional context in comments.

1 Comment

You are most welcome. You may check answer from @BenM as well. It will give you the desired output.
1

How about using end() with explode():

echo end(explode('/', $row['url']));

Output is: xxxxxxxx.

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.