Solved

Get part of string and check configuration

Posted on 2010-09-16
10
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

632 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