Solved

Get part of string and check configuration

Posted on 2010-09-16
10
272 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 74

Expert Comment

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

Open in new window

0
 
LVL 3

Author Comment

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

Regards, JC
0
 
LVL 74

Expert Comment

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

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
You should only need the last posted code.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 3

Author Comment

by:Pedro Chagas
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 450 total points
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now