How to replace last backward slash with forward slash PHP - php

I want to replace my last \ with / on this URL string
C:\wamp\www\chm-lib\sekhelp_out\HTML\AS_BUILD.htm
I have tried this link, but no changes, I am missing something, please correct me where I am wrong.

Here is a solution using PHP's string functions instead of regex.
Do this:
$url = 'C:\wamp\www\chm-lib\sekhelp_out\HTML\AS_BUILD.htm';
$pos = strrpos($url, '\\');
$url = substr_replace($url, '/', $pos, 1);
echo $url;
To get this:
C:\wamp\www\chm-lib\sekhelp_out\HTML/AS_BUILD.htm
Explanation:
Get the position of the last \ in the input string using strrpos()
Replace that with / using substr_replace()
Note
It is important to pass '\\' instead of '\' to strrpos() as the first \ escapes the second.
Also note that you can shorten the code above to a single line if you prefer, but I thought it would be easier to understand as is. Anyway, here is the code as a one-liner function:
function reverseLastBackslash($url) {
return substr_replace($url, '/', strrpos($url, '\\'), 1);
}

You can try exploding the string as an array and imploding after popping off the last part, and connecting it back with a forward slash.
$array = explode('\','C:\wamp\www\chm-lib\sekhelp_out\HTML\AS_BUILD.htm');
$last = array_pop($array);
$corrected = implode('\',$array) . '/' . $last;

The backslash escaping is tricky:
preg_replace('/\\\\([^\\\\]*)$/', '/$1', "C:\\wamp\\www\\chm-lib\\sekhelp_out\\HTML\\AS_BUILD.htm")
You have to escape once for the literal string and once for the regular expression so a single \ needs to be \\\\ (1 x 2 x 2)

Simply use this
str_replace('\\','/','C:\wamp\www\chm-lib\sekhelp_out\HTML\AS_BUILD.htm');

Related

PHP preg_replace replace between slash

I have a PHP function which is supposed to remove a value with its starting / and ending / from a given URL.
for example: remove /page_###/ from http://my-domain.com/search/page_2/order_asc.
the result will be: http://my-domain.com/search/order_asc.
but also the URL may be like this: http://my-domain.com/search/order_asc/page_2. there is no ending / in the end;
function stripDir($url, $mask){
return preg_replace('/' . $mask . '[\s\S]+?/', '', $url);
}
the above function should be able to remove page_5 from following URLS...
stripDir('http://my-domain.com/search/page_5/order_asc', 'page') and stripDir('http://my-domain.com/search/order_asc/page_5', 'page')
The /s in your current regular expression are delimiters, not the starting and ending characters you are looking for. Try changing the delimiter and it should work. Also if the trailing / is optional you should modify the regex I think:
~/page_[\s\S]+?(/|\z)~
or per your function:
~/' . $mask . '[\s\S]+?(/|\z)~
would work for you.
Demo: https://regex101.com/r/sH7bE2/3
Demo of just modified delimiters: https://regex101.com/r/sH7bE2/2
Demo of original regex: https://regex101.com/r/sH7bE2/1
Also since you are checking for the starting and ending / you're going to want to re-place one of those /s.
With substitution demo: https://regex101.com/r/sH7bE2/4
function stripDir($url, $mask) {
return preg_replace('/\/' . preg_quote($mask, '/') . '[^\/]+/', '', $url);
}

Effective way to remove everything after last ? from string

I am trying to strip everything that follows and includes the last ? of a given url. I am currently working with preg_replace but no luck in accomplishing the goal. This is the regex #\/[^?]*$# I am using to single out the last ?. Also is there a faster way by using substr?
Example link:
preg_replace('#\/[^?]*$#', '', $post="www.exapmle.com?26sf213132aasdf1312sdf31")
Desired Output
www.example.com
Here's how to do it with substr and strrpos:
$post = "www.exapmle.com?26sf213132aasdf1312sdf31";
$pos = strrpos($post, '?');
$result = substr($post, 0, $pos);
Add a \? at start of regex instead of \/
\?[^?]*$
\? matches a ?
[^?]*$ matches anything other than a ? until the end of string anchored by $
Example http://regex101.com/r/sW6jE7/3
$post="www.exapmle.com?26sf213132aasdf1312sdf31";
$res=preg_replace('/\?[^?]*$/', '', $post);
echo $res;
will give an output
www.example.com
EDIT
If you want to remove the entire query string from url then a slight modifiation of regex would do the work
\?.*$
which will remove anything followed by a question mark
Simply match everything from the start upto the ? symbol.
preg_match('/^[^?\n]*/', $post="www.exapmle.com?26sf213132aasdf1312sdf31", $match);
echo $match[0];
Output:
www.exapmle.com
Try this its working fine :
$host_url = "www.exapmle.com?26sf213132aasdf1312sdf31";
$part_url = strrpos($host_url, '?');
$result = substr($host_url, 0, $part_url);
echo $result;
Although OP tags regex, Surprisingly nobody suggests explode(), which is much easier.
$post = "www.exapmle.com?26sf213132aasdf1312sdf31";
$tokens = explode('?', $post);
echo $tokens[0]; // www.exapmle.com

Function preg_match() returns nothing

I want to use preg_match() in my code, but the result is nothing ... (or null or empty ?)
$domain = "stackoverflow.com";
$uriToTest = "http://stackoverflow.com/";
$pattern = "/^http(s)?://(([a-z]+)\.)*".$domain."/";
echo preg_match($pattern, $uriToTest);
What is the problem?
If you take a look at your pattern, it's this
/^http(s)?://(([a-z]+)\.)*stackoverflow.com/
The delimiter is used as a matching character, and if you had errors turned on, you'd get a "Unknown modifier" error. So first tip: TURN ERROR REPORTING ON!
To fix it, try using a different delimiter, e.g. {}, as it's easier to read than loads of leaning toothpicks...
{^http(s)?://(([a-z]+)\.)*stackoverflow.com}
The other problem is the dot in the $domain becomes a wildcard match - anytime you insert unknown data into a regex, get in the habit of using preg_quote to escape it, e.g.
$pattern = "{^http(s)?://(([a-z]+)\.)*" . preg_quote($domain, '{') . "}";
(Note - nice catch from stema in the comments: if you use a different delimiter, you must pass that preg_quote. It's clever enough to spot paired delimiters, so if you pass { it will also escape }.)
You're most likely getting an error and preg_match is returning false, as you are not escaping your forward slashes in your expression. Either use something else like a # as the expression delimeter or escape any forward slashes to stop the parser from trying to end the expression (/ should be \/ - or change the / at either end to be #)
//Quick fix to yours
$pattern = "/^http(s)?:\/\/(([a-z]+)\.)*".preg_quote($domain,'/')."/";
//More legible fix
$pattern = '#^https?://(([a-z]+)\.)*'.preg_quote($domain,'#').'#';
Note that you don't need parenthesis around the s in https (unless you're hoping to capture it)
You need to escape your forward slashes and the . in the domain name
$domain = "stackoverflow.com";
$uriToTest = "http://stackoverflow.com/";
$escapedDomain = str_replace('.', '\.', $domain);
$pattern = "/^http(s)?:\/\/(([a-z]+)\.)*".$escapedDomain."/";
echo preg_match($pattern, $uriToTest);
If you were using T-Regx, then this exception would be thrown immediately:
$domain = "stackoverflow.com";
$uriToTest = "http://stackoverflow.com/";
try
{
pattern("/^http(s)?://(([a-z]+)\.)*" . $domain . '/')->match($uriToTest);
}
catch (SafeRegexException $e) {
echo $e->getMessage(); // `Unknown modifier '/'`
}
But also!! T-Regx can automatically add delimiters, so you can go
pattern("^http(s)?://(([a-z]+)\.)*" . $domain)->match($uriToTest);
and it would automatically add a suitable delimiter for you.
$domain = "stackoverflow.com";
$uriToTest = "http://stackoverflow.com/";
$pattern = "^http(s)?://(([a-z]+)\.)*" . $domain . "^";
preg_match($pattern, $uriToTest, $matches);
print_r($matches);

PHP trim() issue

Lets say I have $url="../folder/file" and I want to find and remove the ../ part.
I'm using trim() …
$url = trim($url,"../");
… but it gives me a warning:
Warning: trim() [function.trim]: Invalid '..'-range, no character to the left of '..' on line above
What I did wrong?
what you did wrong was fail to read the manual:
With .. you can specify a range of characters.
<?php
$url="../folder/file";
$url = trim($url,"\.\./");
echo $url;
?>
you can use ltrim
echo ltrim("../folder/file", "./");
or
echo trim("../folder/file", "./");
There is a special syntax in the trim function from php.net/trim that allows you to specify a range, which is what the interpreter believes you are doing because of the '..'
// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
$clean = trim($binary, "\x00..\x1F");
var_dump($clean);
The second argument to trim should be a string of characters that would be stripped, so you should not have to put the '.' twice.
The second argument to the trim function specifies a list of characters to be stripped, not a multi-character string. The function interprets '..' as an operator specifying a range of characters (like a..z or 1..5). You can strip out the '../' in a number of ways, but one easy one is this:
$parts = explode('/', $url);
array_shift($parts);
$url = implode('/', $parts);
I found this function in bottom comments of the trim page on php.net that seem to do what you want
function trimString($input, $string){
$input = trim($input);
$startPattern = "/^($string)+/i";
$endPattern = "/($string)+$/i";
return trim(preg_replace($endPattern, '', preg_replace($startPattern,'',$input)));
}

Replace more than one forward slash

I'd like to replace more than one forward slash with one forward slash.
Examples:
this/is//an//example -> this/is/an/example
///another//example//// -> /another/example/
example.com///another//example//// -> example.com/another/example/
Thanks!
EDIT: This will be used to fix URLs that have more than one forward slash.
try
preg_replace('#/+#','/',$str);
or
preg_replace('#/{2}#','/',$str);
Tips: use str_replace for such a simple replacement AS it
replace all occurrences of the search string with the replacement string
str_replace('/','/',$str);
Reference
You might want to use regex:
$modifiedString = preg_replace('|/{2,}|','/',$strToModify);
I use the {2,} instead of + to avoid replacing single '/'.
Use a regex to replace one or more /-es with /:
$string = preg_replace('#/+#', '/', $string);
I see you want to create a valid url... you might want to check out realpath, or maybe even better the snippet in the first comment:
$path = '../gallery/index/../../advent11/app/';
$pattern = '/\w+\/\.\.\//';
while(preg_match($pattern, $path)) {
$path = preg_replace($pattern, '', $path);
}
// $path == '../advent11/app/'
As you can see this also solves ../-es :)

Categories