Thread: PHP to ASP

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    38
    Rep Power
    5

    PHP to ASP


    Hello,

    Can anyone please help me to translate the followig code from PHP into ASP:
    PHP Code:
    $url $_SERVER['HTTP_REFERER'];

    preg_match("/\&q=(.*?)\&/"$url$matches);

    $str $matches[1];
    echo 
    "str: $str <br />"
    Thank you in advance,
    Roi.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,988
    Rep Power
    9397
    I just spent a few minutes using Google to prove that even someone without ASP knowledge (well, not a lot of) can answer this.

    The second result when I searched for "asp.net referrer" was this page:
    Your web site visitors could come from different sources, like search engines, blogs, banner ads, affiliates, web directories etc.

    To find out which page is generated request to your page, use this code:

    [ C# ]

    string MyReferrer = Request.UrlReferrer.ToString();

    [ VB.NET ]

    Dim MyReferrer As String = Request.UrlReferrer.ToString()
    Then I looked in the MSDN for the HttpRequest.UrlReferrer property.
    Property Value
    Type: System.Uri
    A Uri object.
    Uri has a Query property which is a string.

    Even though I was quite sure there was a better way of doing it, I then looked for ".net regular expression". The first result was also in the MSDN. On the page is has
    Regular Expression Classes
    Provides information and code examples illustrating how to use the regular expression classes.
    Oh! Exactly what I was looking for!
    Scrolling down the words "The CaptureCollection class represents a sequence of captured substrings" caught my eye - as well as the example code posted below it.

    And that's where I stopped, because posting the example code they gave (below is the C# version)
    Code:
        using System;
        using System.Text.RegularExpressions;
    
        public class RegexTest 
            {
            public static void RunTest() 
            {
                int counter;
                Match m;
                CaptureCollection cc;
                GroupCollection gc;
    
                // Look for groupings of "Abc".
                Regex r = new Regex("(Abc)+"); 
                // Define the string to search.
                m = r.Match("XYZAbcAbcAbcXYZAbcAb"); 
                gc = m.Groups;
    
                // Print the number of groups.
                Console.WriteLine("Captured groups = " + gc.Count.ToString());
    
                // Loop through each group.
                for (int i=0; i < gc.Count; i++) 
                {
                    cc = gc[i].Captures;
                    counter = cc.Count;
                    
                    // Print number of captures in this group.
                    Console.WriteLine("Captures count = " + counter.ToString());
                    
                    // Loop through each capture in group.
                    for (int ii = 0; ii < counter; ii++) 
                    {
                        // Print capture and position.
                        Console.WriteLine(cc[ii] + "   Starts at character " + 
                            cc[ii].Index);
                    }
                }
            }
    
            public static void Main() {
                RunTest();
            }
        }
    is what I would normally do for questions like this anyways.


    Today's snark is brought to you by the letter א.

    Comments on this post

    • holodoc agrees : You like FRP games right? :P
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    38
    Rep Power
    5
    Thank requinix, But I don't understand your code...is it possible just to show me the 2-3 lines that I need?
  6. #4
  7. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,428
    Rep Power
    4539
    ASP is a framework, not a language. You need to decide what language you want to use to code your equivalent page, then find out what code does the same as php preg_match.

    And if you're going to use asp.NET you'll probably get better answers in the NET forum, this forum is mostly for classic asp.
    ======
    Doug G
    ======
    The man who doesn't read good books has no advantage over the man who can't read them.
    --Mark Twain
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    38
    Rep Power
    5
    I'm talking about regular ASP
    you know:
    <% %>
  10. #6
  11. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,428
    Rep Power
    4539
    Originally Posted by roice0
    I'm talking about regular ASP
    you know:
    <% %>
    Are you using vbscript? javascript? perlscript? somethingelse? for your asp code?

    There is a regex object available to vbscript, check the ms documentation. And regex is probably part of javascript. If you are using perlscript (highly unlikely but possible) you have perl regex's.

    You'll have to find someone other than me to help with regex though, sorry ..
    ======
    Doug G
    ======
    The man who doesn't read good books has no advantage over the man who can't read them.
    --Mark Twain
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0
    Originally Posted by roice0
    Hello,

    Can anyone please help me to translate the followig code from PHP into ASP:
    PHP Code:
    $url $_SERVER['HTTP_REFERER'];

    preg_match("/\&q=(.*?)\&/"$url$matches);

    $str $matches[1];
    echo 
    "str: $str <br />"
    Thank you in advance,
    Roi.
    something like this ----- untested

    Code:
    <% 
    function findMatch(inpStr)
      Dim oRe, oMatches
      Set oRe = New RegExp
      oRe.Pattern = "/\&q=(.*?)\&/"
      Set oMatches = oRe.Execute(inpStr)
      ' Get the first item in the Matches collection
      findMatch = oMatches(0)
      Set oRe = nothing
    end function
    
    Dim url,str,matches
    url = request.serverVariables("HTTP_REFERER")
    str = findMatch(url)
    
    response.write("str:" & str & "<br />")
    
    %>

IMN logo majestic logo threadwatch logo seochat tools logo