2

I am trying to remove first 51 character of a long URL , I'm using

$sql = $db->Query(some query);
$mysite = $db->FetchArray($sql);
$str = $mysite['url'] ;
$str2 = substr('$str',51);

Above code return blank value, it works fine if i use plan text for $str e.g.

$str = "I am looking for a way to pull the first 100 characters from a string"
$str2 = substr('$str',10);

my url is like "https://dl.dropbox.com/u/55299544/google.html?urlc=http://example.com" i want to get http://example.com from database to show on user page, how can i do this?

2
  • 2
    Have you checked the return value of FetchArray? Perhaps you'll need to index the array like $mysite[0]['url'], try doing a print_r($mysite); Commented Jun 23, 2013 at 8:25
  • $mysite = $db->FetchArray($sql) -> this return full URL value it's correct, how can i do print_r($mysite)? Commented Jun 23, 2013 at 8:27

1 Answer 1

1

You're making this too complicated. If you are trying to get the http://example.com from the long URL then do this.

<?php
sql = $db->Query(some query);
$mysite = $db->FetchArray($sql);
$str = $mysite['url'] ;

$query = parse_url($str, PHP_URL_QUERY );
parse_str($query, $link);

echo $link["urlc"]; //http://example.com
?>
Sign up to request clarification or add additional context in comments.

2 Comments

OMG! it works, thak you very much @Pjack , one ore question -is this code get example.com character count or anything else? means it also work if i change dl.dropbox.com/u/55299544/google.html?urlc= with some other url?
Yes it will work with any other URL with querystring parameters. It parses the querystring parameters of the URL into an array. Just supply the key you need to use. For example if your other url is $str = anothersite.com/?url1=myinfo&url2=yoursite.com. So if you want the value of yoursite.com then you would use the same code above and then do $link["url2"]. Hope that makes sense.

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.