Solved

Get part of string and check configuration

Posted on 2010-09-16
10
274 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 450 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to count occurrences of each item in an array.

786 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