0

I have a string which contains a url. I am trying to extract the url from the additional text in the most efficient way. So far I have been using explode but I have to explode twice and then rebuild the url. Regex is not something I dominate yet so i placed it out of the question(unless it is the best solution). Is there a way to extract the url in one step?

$url = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$strip1  = explode( '&', $url );
$strip2  = explode('=', $strip1[0]);
$result =  $strip2[1].'='.$strip2[2];

result:

http://www.somesite.com/sites/pages/page?id=1545778
0

5 Answers 5

1

Try like this:use preg_split()

$date = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$t =preg_split("/[=&]/",  $date);
echo $t[1]."=".$t[2]; //output: http://www.somesite.com/sites/pages/page?id1545778
Sign up to request clarification or add additional context in comments.

4 Comments

@MaryCoding this may help you :)
unfortunately split is deprecated.
thanks, this works perfect as is simple and doesn't involve regex
@MaryCoding This is actually using a regex. /[=&]/ is a regular expression.
0
$strip1  = explode( '/url?q=', $url );

Use this regex $strip1

^((http):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$

you will get an array of sections in the url

Comments

0

Ugly one-step regex-free solution.

$url = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$result = substr( $url, strpos( $url, '=' ) + 1, strpos( $url, '&' ) - strpos( $url, '=' ) - 1 );
echo $result;

And cleaner two-step variation.

$url = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$start = strpos( $url, '=' ) + 1;
$result = substr( $url, $start, strpos( $url, '&' ) - $start );
echo $result;

Somewhat less-ugly regex solution.

$url = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$result = preg_replace( '/[^=]*=([^&]*).*/', '${1}', $url );
echo $result;

Both produce the following output.

http://www.somesite.com/sites/pages/page?id=1545778

1 Comment

Looks like the regex by far seems to be cleaner than the other ways
0

Technically, that second ? in the URL should be URL encoded, but we can get around that. Use parse_url to get the query, then replace ? with a URL encoded version using str_replace. After this, you will have a valid query that you can parse using parse_str.

$query = parse_url($url, PHP_URL_QUERY);
$query = str_replace("?", urlencode("?"), $query);
parse_str($query, $params);
echo $params['q'];

// displays http://www.somesite.com/sites/pages/page?id=1545778

Comments

0
$url = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";

$strip3  = current(explode('&',end(explode('=', $url,2))));
print_r ($strip3);    //output  http://www.somesite.com/sites/pages/page?id=1545778

1 Comment

try this one using explode.

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.