SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    htaccess and php preg_match ??

    Hey,

    I'm just wondering how to use the preg_match function in php to meet the variables of the htaccess

    strictly to the point, hope you understand my silly english

    look in htaccess we use things like

    Code:
    RewriteRule ^([^/]+)/$ ./test.php?url=$1
    what i'm looking for is what is the variable which we use in the preg_match function to meet the code
    Code:
    ([^/]+)/
    and how to add many variables like

    Code:
    RewriteRule ^([^/]+)/p([0-9]+).html$ ./test.php?url=$1&page=$2
    what is the equivalent of these variables in the preg_match function ?

    cuz i've seen alot of the examples like this one

    PHP Code:
    preg_match("/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i"$string); 
    but i didn't understand what's in it.

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Location
    Canada, Alberta
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, the PHP preg_match looks like it checks if it's a valid email address.

    Are you asking how to pass matches into a variable in php?

    Code:
    preg_match($pattern, $string, $match);
    //if there's a match, it will be passed into $match variable as an array
    the htaccess examples you gave above is more of a preg_replace function in php where $n (n is any number). here's an example from http://www.php.net/manual/en/function.preg-replace.php

    Code:
    $string = 'April 15, 2003';
    $pattern = '/(\w+) (\d+), (\d+)/i';
    $replacement = '${1}1,$3';
    echo preg_replace($pattern, $replacement, $string);
    
    //output: April1,2003
    each $n variable is based on each pattern/subpattern

  3. #3
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i know that the example i wrote above was for checking if an e-mail is valid

    and i know how to use the preg_match function but my question is what are the patterns that's equivalent to the htaccess codes which i can use in the preg_match function ??

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    To have PHP pick up those variables from the previous mod_rewrite you showed us, you would simply do this in your test.php

    test.php
    PHP Code:
    <?php

    echo 'page is ' $_GET['page'];
    echo 
    'url is ' $_GET['url'];

  5. #5
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    To have PHP pick up those variables from the previous mod_rewrite you showed us, you would simply do this in your test.php

    test.php
    PHP Code:
    <?php

    echo 'page is ' $_GET['page'];
    echo 
    'url is ' $_GET['url'];
    well thank you but i already know that, actually every php beginner knows that

    my question is what is the pattern that's equivalent to this code "([^/]+)", this code "([^/]+)/p([0-9]+).html", and so on.

    i just want to redirect all the links to the test.php file which in turn match the requested link to the exact pattern i want then send data which i got from the link pattern to another page. like in vbseo plugin all links are redirected to the vbseo.php file.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are you saying you want PHP to redirect the user depending on the value of $_GET['url'], and the pattern you are looking to match is something like p123.html ?

    If that is not what you mean, then try and spend a bit more time and attention in giving us an actual specific example of what you do want, and maybe someone will be able to help you out.

  7. #7
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guess i've pissed you off and i'm sorry didn't mean to

    i'm not depending on the value of $_GET['url']

    i'm depending on the value of $_SERVER['REQUEST_URI']

    guess now there's no mystery in my question

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Im not pissed off, but I am getting exasperated because I fail to understand your problem. Dont worry, a forum is sometimes not the best method for getting ideas across -- time lags, language differences etc.

    Let's try this:

    Give us a concrete example of a value that $_SERVER['REQUEST_URI'] would hold (just make one up that typifies your problem).

    Then explain what the regex has to extract from that - don't describe it in terms, show us exactly the text or decision that has to be extracted from that $_SERVER['REQUEST_URI']

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    ([^/]+)/

    Is a valid preg_match pattern, which reads "One or more of anything that isnt a /, followed by a /".

    How to decode it:
    () is a subpattern. the / on the end is a literal /. So we've dealt with those. Remaining pattern: [^/]+
    [] is a character class. + after a character class means "One or more of the preceding class".
    ^ at the beginning of a character class means "negate the class"
    the / is again a literal /.
    So now that you've broken it down, build it back up:
    ^/ "Not a slash."
    [^/]+ "One or more not-a-slash-es"
    ([^/]+)/ "One or more not-a-slashes followed by a slash.", which, reworded, is "One or more of anything that isnt a /, followed by a /".

    http://us.php.net/manual/en/referenc...ern.syntax.php

    EDIT: Note that the ^ has another meaning if used outside of a character class.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  10. #10
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok then, I'm working on a cms script and one of the urls (the topic one) should be like

    Code:
    http://www.example.com/cmsdir/category/topic1/topic-name.html
    when i redirect the urls to the test.php file which includes the following simple code

    PHP Code:
    <?php

    echo $_SERVER['REQEST_URI'];

    ?>
    it prints me out

    Code:
    /cmsdir/category/topic1/topic-name.html
    and the htaccess file has the following code

    Code:
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ test.php [L,QSA]
    i want to use the preg_match function to match the requested url to an exact pattern that is
    Code:
    /category/topic1/topic-name.html
    and if it matches this pattern the test.php file sends the variables (that i extract from the url such as $category,$id and $name) to another file which has the query codes

    and thank you for your patience

  11. #11
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    So... the request URI contains /cmsdir/category/topic1/topic-name.html, and you want to capture category, topic1, and topic-name.

    preg_match("~/cmsdir/([^/]+)/([^/]+)/([^/]+).html~",$_SERVER['REQEST_URI'],$matches);

    $matches[1] = category
    $matches[2] = topic1
    $matches[3] = topic-name
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  12. #12
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    ([^/]+)/

    Is a valid preg_match pattern, which reads "One or more of anything that isnt a /, followed by a /".

    How to decode it:
    () is a subpattern. the / on the end is a literal /. So we've dealt with those. Remaining pattern: [^/]+
    [] is a character class. + after a character class means "One or more of the preceding class".
    ^ at the beginning of a character class means "negate the class"
    the / is again a literal /.
    So now that you've broken it down, build it back up:
    ^/ "Not a slash."
    [^/]+ "One or more not-a-slash-es"
    ([^/]+)/ "One or more not-a-slashes followed by a slash.", which, reworded, is "One or more of anything that isnt a /, followed by a /".

    http://us.php.net/manual/en/referenc...ern.syntax.php

    EDIT: Note that the ^ has another meaning if used outside of a character class.
    well i tried the code ([^/]+)/ in the test file and it get's me the next error

    Code:
    Warning: preg_match() [function.preg-match]: Unknown modifier '/' in C:\xampp\htdocs\cmsdir\test.php on line 3
    and here's the content of the test.php file

    PHP Code:
    <?php

    Preg_Match
    ('([^/]+)/'$_SERVER['REQUEST_URI'], $Match);

    if(isset(
    $Match[1])) {
    echo 
    "Good";
    }else {
    echo 
    "Bad";
    }

    ?>

  13. #13
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    So... the request URI contains /cmsdir/category/topic1/topic-name.html, and you want to capture category, topic1, and topic-name.

    preg_match("~/cmsdir/([^/]+)/([^/]+)/([^/]+).html~",$_SERVER['REQEST_URI'],$matches);

    $matches[1] = category
    $matches[2] = topic1
    $matches[3] = topic-name
    well let me try this one

  14. #14
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    So... the request URI contains /cmsdir/category/topic1/topic-name.html, and you want to capture category, topic1, and topic-name.

    preg_match("~/cmsdir/([^/]+)/([^/]+)/([^/]+).html~",$_SERVER['REQEST_URI'],$matches);

    $matches[1] = category
    $matches[2] = topic1
    $matches[3] = topic-name
    Great One, this is my pattern
    when i copied the code it gave me an error then i realized that REQUEST_URI is missing the U , anyway it's working
    many thanks <3 <3

  15. #15
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another simple question

    when i use code like

    PHP Code:
    preg_match("~/news/?~",$_SERVER['REQUEST_URI'],$matches); 
    when i use the link like "http://www.example.com/cmsdir/news/" it matches

    and if i use it like "http://www.examples.com/cmsdir/news/anything-here" it matches too :S

    is there anyway to match only the "http://www.example.com/cmsdir/news/" link ??

  16. #16
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    "~/news/?$~"
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  17. #17
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    "~/news/?$~"
    You are Awesome thank you <3


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •