I'm new in regular expressions - so, here's my simple question:

I'd like to get the fist part of an URL that contains a query string or a '#':

e.g.
"www.somewhere.com/test/index.php?q=nothing"
"www.somewhere.com/test/index.php#someanchor

"www.somewhere.com/test/index.php"

I do not want to use parse_url() as I wouldn't be able to check for the '#'

thanks
Philip

    use $REQUEST_URI this will give you the URL user entered to read to the file.

    then use explode to seperate the URL by #. e.g.

    $value = explode('#', $REQUEST_URI);

    so, $value[0] will contain /test/index.php
    and $value[1] will contain someanchor

    hope that helps.
    Daarius...

      thanks Daarius

      I know how to use the explode() function. - Actually I d'like to know what would be the regular expression to cut the URL with ereg() - something that works for '#' and '?'.

      something like "(.+)(\?|#)(.$)" - hehe, got no idea, but I would really like to learn some regex's!

      philip

        try:
        <?php
        $url="http://www.someserver.net/index.php?someparams";
        ereg("([\?#]*)",$url,$arg);
        echo $arg[1];
        ?>

        • the () means to remember result of regex in ()
        • [] means range, eg. either of ? or #
        • ^ means NOT

        so, that regex means:
        remember all the continual characters until ? or #

          GREAT!! Thanks a lot! good explanation - you gave me first lessons in regex!

            Write a Reply...