Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get part of string and check configuration

Posted on 2010-09-16
10
Medium Priority
?
280 Views
Last Modified: 2012-05-10
Hi E's, I use "$string=$_SERVER['REQUEST_URI'];" to get the part of URL address after the domain.
The output of $string can be:
/en/file.php?value=value (en can be anything with 2 characters, like pt, es, ru, 2 characters)
/pt/folder/file.php?value=value (pt can be anything too, 2 characters, the folder can have any name)

I need get the part of $string after the acronym, like /index.php?value=value or /folder/index.php?value=value. If $string have a configuration different of:
/2 characters just alpha NOT numeric/file.php
/2 characters just alpha NOT numeric/file.php?variable=get
/2 characters just alpha NOT numeric/any name of folder/file.php
/2 characters just alpha NOT numeric/any name of folder/file.php?variable=get
Output a error.

Regards, JC
0
Comment
Question by:Pedro Chagas
  • 5
  • 5
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33694005
Can't you just do a substring call?
$rest = substr($str, 3)

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33694221
Hi, but how I see if the configuration of string is the correct?

Regards, JC
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33694281
Ok then:
preg_match('#^/[a-z]{2}/(?:\w+/)?file\.php(?:\?\w+=\w+)?#', $string, $match);

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33694570
Hi, just one thing, for the goal of this question, I have to use your two solutions?
With substr I get the part of URL I want and with preg_match I check the configuration?

Regards, JC

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33695090
You should only need the last posted code.
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33695597
Hi, in my last post. I ask because I try to use the example of preg_match for both ends, but that example the output is always the same of $string variable.
In practice (for example):
$string= "/es/file.php?variable=variable";
output was /es/file.php when should be /file.php?variable=variable
$string= /es/folder/file.php
output /es/folder/file.php when should be /folder/file.php

In other hand I can use the example of preg_match for determinate the correct configuration of URL, like you can see in the snippet code. And work well!

But for both ends don't work!
Don't forget that file.php can be any file and any extension (any file . any extension), please review that part too.

Regards, JC
$string= "/es/folder/file.php";
preg_match('#^/[a-z]{2}/(?:\w+/)?file\.php(?:\?\w+=\w+)?#', $string, $match);
if($match[0] == ""){
	echo "wrong URL";
} else {
	echo "The URL:$match[0] is well config";
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33695798
Try this modification:
preg_match('#^(?<=/[a-z]{2})/(?:\w+/)?file\.\w+(?:\?\w+=\w+)?#', $string, $match);

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33696677
Hi, I try the modification, but the output now is nothing. I try with different address, like $string= "/es/file.php"; or $string= "/es/folder/file.php";.
The RE continue considering  the "file", like I tell you before <<can be any file and any extension (any file . any extension>>, file can have any name (file1.php, file__2.swf, index.htm index.php etc).
Please review the RE.

Regards, JC
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1800 total points
ID: 33696944
I put the BOL anchor in the wrong place. I checked this and it appears to give you the result you desire:
preg_match('#(?<=^/[a-z]{2})/(?:\w+/)?\w+\.\w+(?:\?\w+=\w+)?#', $string, $match);

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33697115
Hi @kaufmed, thanks again. This week you answer 3 or 4 question to me. Thanks again!
I put a complete example in snippet code, for beginners like me, understand the code too.

Regards, JC
<?
$string = $_SERVER["REQUEST_URI"];
preg_match('#(?<=^/[a-z]{2})/(?:\w+/)?\w+\.\w+(?:\?\w+=\w+)?#', $string, $match);
if($match[0] == ""){
	echo "Wrong URL";
} else {
	echo "The URL:$match[0] is correct";
}
?>

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question