I need to remove the first forward slash inside link formatted like this:
/directory/link.php
I need to have:
directory/link.php
I'm not literate in regular expressions (preg_replace?) and those slashes are killing me..
I need your help stackoverflow!
Thank you very much!
Just because nobody has mentioned it before:
$uri = "/directory/link.php";
$uri = ltrim($uri, '/');
The benefit of this one is:
compared to the substr() solution: it works also with paths that do not start with a slash. So using the same procedure multiple times on an uri is safe.
compared to the preg_replace() solution: it's certainly much more faster. Actuating the regex-engine for such a trivial task is, in my opinion, overkill.
preg_replace('/^\//', '', $link);
If it's always the first character, you won't need a regex:
$uri = "/directory/link.php";
$uri = substr($uri, 1);
Related
I have a series of URLs like so:
http://www.somesite.com/de/page
http://www.somesite.com/de/another
http://www.somesite.com/de/page/something
http://www.somesite.com/de/page/bar
I need to search the block of text and pull the language and am using a regex like so:
/(de|en|jp)/
I'm trying to find and replace, via preg_replace and including the forward slashes:
/de/
/en/
/jp/
However, this doesn't work and does not include the slashes. I've tried escaping the slashes with \, \\. I've tried placing the needle in preg_quote but this breaks the alternation.
I feel like I am missing something very simple here!
edit:
Full function call:
preg_replace("/(de|en|jp)/", "/".$newLang."/", $url);
--
(tagged magento and wordpress as I am trying to solve an issue with unifying the navigation menu when both CMSes are multilingual)
You don't have to use slashes as delimiters, but you have to have some delimiter. Try this:
if( preg_match("(/(de|en|jp)/)",$url,$m)) {
$lanuage = $m[1];
}
You can use a different delimiter, such as %.
if (preg_match('%/(de|en|jp)/%', $url, $match)) {
$lang = $match[1];
}
That should help you, just modify what you have :).
I'm working in PHP with friendly URL paths in the form of:
/2011/09/here-is-the-title
/2011/09/here-is-the-title/2
I need to standardize these URL paths to remove anything after the 4 slash including the slash itself. The value after the 4th slash is sometimes a number, but can also be any parameter.
Any thoughts on how I could do this? I imagine regex could handle it, but I'm terrible with it. I also thought a combination of strpos and substr might be able to handle it, but cannot quite figure it out.
You can use explode() function:
$parts = explode('/', '/2011/09/here-is-the-title/2');
$output = implode('/', array_slice($parts, 0, 4));
Replace
%^((/[^/]*){3}).*%g
with $1.
see http://regexr.com?2vlr8 for a live example
If your regex implementation support arbitrary length look-behind assertions you could replace
(?<=^[^/]*(/[^/]*){3})/.*$
with an empty string.
If it does not, you can replace
^([^/]*(?:/[^/]*){3})/.*$
with the contents of the first capturing group. A PHP example for the second one can be found at ideone.com.
you could also use a loop:
result="";
for char c in URL:
if(c is a slash) count++;
if(count<4) result=result+c;
else break;
is there a great way to extract subdomain with php without regex ?
why without regex ?
there are a lot of topic about this, one if them is Find out subdomain using Regular Expression in PHP
the internet says it consumes memory a
lot, if there is any consideration or
you think better use regex ( maybe we
use a lot of function to get this
solution ) please comment below too.
example
static.x.com = 'static'
helloworld.x.com = 'helloworld'
b.static.ak.x.com = 'b.static.ak'
x.com = ''
www.x.com = ''
Thanks for looking in.
Adam Ramadhan
http://php.net/explode ?
Just split them on the dot? And do some functions?
Or if the last part (x.com) is the same everytime, do a substring on the hostname, stripping of the last part.
The only exception you'll have to make in your handling is the www.x.com (which technically is a subdomain).
$hostname = '....';
$baseHost = 'x.com';
$subdomain = substr($hostname, 0, -strlen($baseHost));
if ($subdomain === 'www') {
$subdomain = '';
}
Whoever told you that regexes "consume a lot" was an idiot. Simple regexes are not very cpu/memory-consuming.
However, for your purpose a regex is clearly overkill. You can explode() the string and then take as many elements from the array as you need. However, your last example is really bad. www is a perfectly valid subdomain.
You can first use parse_url http://www.php.net/manual/de/function.parse-url.php
and than explode with . as delimiter on the host http://www.php.net/manual/de/function.explode.php
I would not say it is quicker (just test it), but maybe this solution is better.
function getSubdomain($host) {
return implode('.', explode('.', $host, -2));
}
explode splits the string on the dot and drops the last two elements. Then implode combines these pieces again using the dot as separator.
I have a regular expression that I use to reduce multiple slashes to single slashes. The purpose is to read a url that is previously converted to a human readable link using mod_rewrite in apache, like this :
http://www.website.com/about/me
This works :
$uri = 'about//me';
$uri = preg_replace('#//+#', '/', $uri);
echo $uri; // echoes 'about/me'
This doesn't work :
$uri = '/about//me';
$uri = preg_replace('#//+#', '/', $uri);
echo $uri; // echoes '/about/me'
I need to be able to work with each url parameter alone, but in the second example, if I explode the trailling slash, it would return me 3 segments instead of 2 segments. I can verify in PHP if any if the parameters is empty, but as I'm using that regular expression, it would be nice if the regular expression already take care of that for me, so that I don't need to worry about segment validation.
Any thoughts?
str_replace may be faster in this case
$uri = str_replace("//","/",$uri)
Secondly: use trim: http://hu.php.net/manual/en/function.trim.php
$uri = trim($uri,"/");
This converts double slashes in a string to a single slash, but the advantage of this code is that the slashes in the protocol portion of the string (http://) are kept.
preg_replace("#(^|[^:])//+#", "\\1/", $str);
How about running a second replace on $uri?
$uri = preg_replace('#^/#', '', $uri);
That way a trailing slash is removed. Doing it all in one preg_replace beats me :)
Using ltrim could also be a way to go (probably even faster).
I need to be able to work with each
url parameter alone, but in the second
example, if I explode the trailling
slash, it would return me 3 segments
instead of 2 segments.
One fix for this is to use preg_split with the third argument set to PREG_SPLIT_NO_EMPTY:
$uri = '/about//me';
$uri_segments = preg_split('#/#', $uri, PREG_SPLIT_NO_EMPTY);
// $uri_segments[0] == 'about';
// $uri_segments[1] == 'me';
you can combine all three alternatives into one regexp
$urls = array(
'about/me',
'/about//me',
'/about///me/',
'////about///me//'
);
print_r(
preg_replace('~^/+|/+$|/(?=/)~', '', $urls)
);
You may split the string via preg_split instead, skipping the sanitizing altogether. You still have to deal with the empty chunks, though.
Late but all these methods will remove http:// slashes too, but this.
function to_single_slashes($input) {
return preg_replace('~(^|[^:])//+~', '\\1/', $input);
}
# out: http://localhost/lorem-ipsum/123/456/
print to_single_slashes('http:///////localhost////lorem-ipsum/123/////456/');
I need a regular expression to extract from two types of URIs
http://example.com/path/to/page/?filter
http://example.com/path/to/?filter
Basically, in both cases I need to somehow isolate and return
/path/to
and
?filter
That is, both /path/to and filter is arbitrary. So I suppose I need 2 regular expressions for this? I am doing this in PHP but if someone could help me out with the regular expressions I can figure out the rest. Thanks for your time :)
EDIT: So just want to clearify, if for example
http://example.com/help/faq/?sort=latest
I want to get /help/faq and ?sort=latest
Another example
http://example.com/site/users/all/page/?filter=none&status=2
I want to get /site/users/all and ?filter=none&status=2. Note that I do not want to get the page!
Using parse_url might be easier and have fewer side-effects then regex:
$querystring = parse_url($url, PHP_URL_QUERY);
$path = parse_url($var, PHP_URL_PATH);
You could then use explode on the path to get the first two segments:
$segments = explode("/", $path);
Try this:
^http://[^/?#]+/([^/?#]+/[^/?#]+)[^?#]*\?([^#]*)
This will get you the first two URL path segments and query.
not tested but:
^https?://[^ /]+[^ ?]+.*
which should match http and https url with or without path, the second argument should match until the ? (from the ?filter for instance) and the .* any char except the \n.
Have you considered using explode() instead (http://nl2.php.net/manual/en/function.explode.php) ? The task seems simple enough for it. You would need 2 calls (one for the / and one for the ?) but it should be quite simple once you did that.