Get part of string and check configuration

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
LVL 3
Pedro ChagasWebmasterAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
käµfm³d 👽Connect With a Mentor Commented:
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
 
käµfm³d 👽Commented:
Can't you just do a substring call?
$rest = substr($str, 3)

Open in new window

0
 
Pedro ChagasWebmasterAuthor Commented:
Hi, but how I see if the configuration of string is the correct?

Regards, JC
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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

Open in new window

0
 
Pedro ChagasWebmasterAuthor Commented:
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
 
käµfm³d 👽Commented:
You should only need the last posted code.
0
 
Pedro ChagasWebmasterAuthor Commented:
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
 
käµfm³d 👽Commented:
Try this modification:
preg_match('#^(?<=/[a-z]{2})/(?:\w+/)?file\.\w+(?:\?\w+=\w+)?#', $string, $match);

Open in new window

0
 
Pedro ChagasWebmasterAuthor Commented:
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
 
Pedro ChagasWebmasterAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.