<a href="/index.php?mid=test&document_url=15424">

에서 15424 만 뽑아내고 싶은데

 

/<a[^>]* href=(/[^0-9]*/s)\\1[^>]*>/is

이렇게 하면 다른 링크들까지 모조리 나와버리는데 document_url 뒤에 있는 숫자만 뽑으려면 

정규표현식을 어떻게 써야될까요

  • profile

    /<a[^>]* href="[^">]*document_srl=([0-9]+)[^">]*"[^>]*>/is

     

    document_url이 아니라 document_srl입니다 ㅎㅎ

    근데 만약 짧은주소를 사용하신다면 저것도 안 되겠네요.

  • ?
    정규표현식도 좋지만, 가끔은 고전적인 방법으로 접근하는 것이 쉬울 때도 있습니다.
    그냥 참고하시라고 올려봅니다.

    <?php
    function getData($url, $retrieve_key) {
    if(!$url) {
    return NULL;
    }

    $query = parse_url($url, PHP_URL_QUERY);
    if(!$query) {
    return NULL;
    }

    $pairs = explode('&', $query);

    foreach($pairs as $pair) {
    list($key, $value) = explode('=', $pair);

    if($key === $retrieve_key) {
    return $value;
    }
    }

    return NULL;
    }

    echo getData('/index.php?mid=test&document_srl=15424', 'document_srl');
    ?>
  • ? profile

    글 쓰신 분은 링크 주소가 아니라 HTML 소스 전체를 갖고 작업하셔야 하는 상황인 것 같으니

    일단 정규식으로 링크를 모두 뽑아낸 후, 이런 함수를 사용해서 문서번호를 추출해 내도 되겠네요.

     

    explode 이하는 parse_str 함수로 대체하면 더욱 좋습니다 ㅎㅎ

  • profile
    /<a[^>]*href="([^=]|[^0-9])*=([^a-zA-Z&])+([^=]|[^0-9])*">/is 라고 생각했지만...
    document_srl을 문자열으로 넣는다는 생각을 못했...ㅠ
  • profile
    저도 어렵네요 ㅠㅠ
  • profile
    (?:<a[^>]* href="[^">]*document_srl=)?([0-9]+)[^">]*"[^>]*>
  • profile
    preg_match("/document_url=([0-9]+)/", $input_line, $output_array);
    echo $output_array[1];