1

This is my Image Url PHP Code.

$GetImage = 'https://lh6.ggpht.com/hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw=w124';

preg_match_all("/https://\w\w\d.\w+.com/[\w-]+=\w\d{2,3}/", $GetImage, $Result, PREG_SET_ORDER);

its working for me, but i want to extract "[\w-]" pattern results, in other words, i want to extract "hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw" this string from my image Url...

Please anybody help my to solve this problem....

thanks

3
  • 1
    Em, you did read the documentation of preg_match? You did try putting braces (()) around that [\w-]+ sequence? Commented Feb 23, 2013 at 9:01
  • i will catch this string in $Result[0] ? Commented Feb 23, 2013 at 9:09
  • !! You DID read the documentation? Commented Feb 23, 2013 at 9:24

2 Answers 2

1

I feel it's overkill to try to match the entire URL using a regular expression. I suggest you parse the URL first using PHP's built-in function parse_url().

<?php

$str = 'https://lh6.ggpht.com/hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw=w124';

// Parse the URL before applying a regex. Only get the path part. Use substring to remove the leading slash
$path = substr( parse_url( $str, PHP_URL_PATH ), 1 );

$pattern = '/([^=]+)/';
$matches = array();

if ( preg_match( $pattern, $path, $matches ) ) {
    // Regex matched

    $id = $matches[1];

    // Outputs: string 'hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw' (length=66)
    var_dump( $id );
}

?>

Note that the snippet does not check the domain name. You can easily adjust the script to do so by not limiting the parse_url() function to only return the path, but also the other parts.

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

Comments

0

Try like this

$GetImage = 'https://lh6.ggpht.com/hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw=w124';
preg_match_all('#https://.*\.com/([\w-]+=\w\d{2,3})#iU', $GetImage, $match, PREG_SET_ORDER);
print_r($match);

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.