ET is correct. To further help, here is the regex broken down:
The beginning of the regex pattern:
“/
Parenthesis capture content. The dot matches any character (except for line breaks). The “+” is called a quantifier (it tells you how many time the previous element is repeated. “+” basically means “one or more”). The question mark makes it lazy (as soon as it finds its first match it stops).
(.+?)
”\/” represents a forward slash (the initial backslash is used as an escape). “*” is a quantifier like the plus sign above (it basically means “0 or more”). The dollar sign is an anchor (it represents the end).
\/*$
The ending of the regex pattern:
/”
This is what the original string is getting replaced with. The “\\1” simply means the part of the text that matched the first parenthesis (which would be all of the text except for the closing forward slash(es)). “/” just adds the closing forward slash to make sure that there is always a forward slash.
“\\1/”
I figured I would work it out for anybody who isn’t very familiar with regular expressions (they are very powerful). I highly recommend www.regular-expressions.info - it will teach you just about everything you need to know to get up and running with regex.
Hope this helps - it would be great to see it added.
@sophistry
This part of the code is doing something slightly different than what was happening in Pagination:
if (ereg("/$", $pref) === FALSE)
{
$pref .= '/';
}
Basically it checks if the $pref ended with a slash. If it does not, then it adds a slash.
Geert’s code mimics the regex in the Pagination file.
// Add a trailing slash to the base URL if needed
$this->base_url = rtrim($this->base_url, '/') .'/';
Basically it makes sure that $this->base_url only ends with one forward slash.
To give an example, if the input string looked like this: “asdf///” you would get different results from the two functions:
ereg result: “asdf///”
rtrim result: ““asdf/”
It is probably a minor change but I think it would probably need testing before dropping in (it’s not the exact same code just executing quicker).
As Martin mentioned, there are probably a lot of regex expressions throughout CI that could be improved.