1
NOTIFYING
The solutions below, only will not have great serventias, with them it is necessary to have previously already implemented URL friendly, and also checks of display of the contents of the system that will be implemented, Here I will only give examples of entries of full addresses received in the URL, for each project in specific the system postterm should already have a treatment of receiving parameter so with mine or try to base on what the function can offer to implement it. But after all, what was her goal then?
If your system is no longer downloading data if you are asked for a "my-site//an*i#maç! to" and with it brings something like "page not found..."
Great! The function will serve and will try to redirect if the server itself no longer blocks with error 400
OBJECTIVES:
1) When no search parameters are available via GET type "?"
meu-site///an*i#maç!ao//##minha-postagem/
for that reason!
meu-site/animação/minha-postagem
utilise:
if(preg_replace('/[^A-Za-z0-9-\/]/','',$url)){
//remove caracteres especiais permitindo hífen,e barra
$nova_url= preg_replace('/[^A-Za-z0-9-\/]/','',$url);
}
if(preg_replace(('/(\/)\1+/',$url)){
//remove varias barras seguidas
$nova_url= preg_replace('/(\/)\1+/','$1', $url);
}
***** Improvement Request: (could remove bar if there were at last position) *****
2) When I have only the search parameter via GET of type "?" without paging:
meu-site/@#$%%@?sWXX=palavra
for that reason!
meu-site/?s=palavra
Implemented (function created and explained below)
preg_match('/^.*?\?.*?s.*?=$/', $str)) {return "?s=";}
3) Now when I have paging and search parameter together
meu-site/!anim a cao/!!!page/@#$%%@?sWXX=palavra
for that reason!
meu-site/animacao/page/2?s=palavra
Implemented (function created and explained below)
if (preg_match('{^((?:[a-zA-Z][^a-zA-Z]*)+)/.*?p.*?a.*?g.*?e.*?/.*?(\d+).*?\?.*?s.*?=(.*)$}', $str, $matches)) {
$trecho1 = preg_replace('/[^a-zA-Z]/', '', $matches[1]);
return "{$trecho1}/page/{$matches[2]}?s={$matches[3]}";
}
Anyway, I know that there are many possibilities of data entries, but as I said these expressions serve to help a URL typed "erroneously" to arrive at the intended content, let’s say in passing improving even a possible duplication of content! This is what I needed but if you can improve, those who want to help modify my own functions, comment and help improve the code!
BELOW COMES THE REQUEST AND EXPLANATIONS
I need a regular PHP expression that preserves my search parameter. My parameter on the site calls s
(abbreviation of search, just like most websites), soon when I go to do a search on the site via GET the browser will interpret like this:
meu-site/?s=
Basically what I need is if there is anything before the parameter ?s=
or anything in the middle of it, the preg_replace
remove and return me the string in the clean way above.
Come on then, what do I need that expression to do:
First expression
If there is no parameter other than "? s=" in the string do nothing if it is like this!
?s=
Otherwise remove everything that is between it (after no need, the browser understands it as the search word), and everything that is before it! Allowing only characters a-z
, numbers 0-9
, 1 single bar (not followed) and hyphen, like the expressions I made below ex:
Entree:
to) ???!!?s=
b) ///()!?s=
c) !?s!=
Exit:
?s=
Second expression
- Now if you have more parameters in the URL than
s
(don’t worry I already check it all) just interpret the input string.
NOTE: for paging, I don’t even use the bar after the pagination number...
animacao/page/2?s=
Entree:
1) animacao/!pa*ge)*&/2?s=
2) animacao////!?page))/2?s!=
3) animacao/!?pa_ge))/!@2?s*!=
Exit:
animacao/page/2?s=
These expressions I made work well throughout the site, only only if there is no search parameter via GET as for example "? s=", finally I use to treat:
<?php
//remove caracteres especiais,permitindo apenas / e hífen
preg_replace('/[^A-Za-z0-9-\/]/','',$url);
//remove barras varias barras
preg_replace('/(\/)\1+/','$1', $url);
//remove várias interrogações seguidas
preg_replace('/([?])\1+/','$1', $url);
?>
Update of request in relation to comments
The function that the User "hkotsubo" offered is according to what I need, reading his excellent explanation about it, I tested and identified an improvement if possible.
The value that the expression receives in "$Matches[1]", I believe that in this case the word "animacao" (I may be wrong) is not working house have space before, between or after the word.... and special characters...
What I need:
If possible allow hyphenation, and if there is space or special characters in any position remove. The hyphen I think I was able to add in this "a-za-Z-" section now the space needs a treatment...
Input:(space)
1) ani mac ao /page/2?s=
2) a!!@*ni-ma-cao!/page/2?s=
Exit:
animacao/page/2?s=
Doubts:
sQ!=
flippeds=
- why theQ
some? Shouldn’t it besQ=
? Or you can only have a parameter calleds
? And why![]}page!)///2?s=
flippedpage/2?s=
, but!!@SSDwe?s=
flipped?s=
? What is the criterion forpage
not disappear andSSDwe
disappear? You can only have thes
, or may have?s=a&xyz=abc&etc=123....
? Anyway, it was not clear to me all the criteria...– hkotsubo
I also got the question of @hkotsubo .... I think you could better exemplify.. Type: If the url looks like this:
meu-site/!!@SSDwe?s=
I want it to stay that way:meu-site/?s=
for example... if she’s "roasted" ... I want it to stay that way... and such...– Andrei Coelho
yes I’ll explain it to you...
– Caio Lourençon
Edited, anything ask me.
– Caio Lourençon