PHP preg_replace replace between slash - php

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);
}

Related

How to replace last backward slash with forward slash 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');

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);

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 :)

How to remove ETX character from the end of a string? (Regex or PHP)

How can I remove the ETX character (0x03) from the end of a string? I'd either like a function, otherwise I can write a regular expression, all I want to know is how to match the ETX character in a regular expression? trim() doesn't work.
To complete Charles' answer, I had a case where the ETX character was not at the end of the line so I had to use the following code:
$string = preg_replace('/\x03/', '', $string);
Just remove the $ sign.
Have you tried rtrim function
http://php.net/manual/en/function.rtrim.php
The ETX is a control character, which means we have to go through some extra effort to bring it into existence.
From the PHP interactive prompt:
php > $borked = "Hello, have an ETX:" . chr(3);
php > print_r($borked);
Hello, have an ETX:
php > var_export($borked);
'Hello, have an ETX:'
php > echo urlencode($borked);
Hello%2C+have+an+ETX%3A%03
php > var_dump( preg_match('/' . chr(3) . '/', $borked) );
int(1)
php > var_dump( preg_match('/' . chr(3) . '$/', $borked) );
int(1)
php > echo urlencode( preg_replace('/' . chr(3) . '$/', '', $borked) );
Hello%2C+have+an+ETX%3A
In other words, you can basically just inject the character into the regex string. Now, this might backfire. This technique may backfire based on character sets and other horrible things.
In addition to chr(3), you could also try urldecode('%03') to produce the character. You can also use a special escape syntax, as listed on the PCRE syntax pages: /\x03$/
Here's a function for you, using that last method:
function strip_etx_at_end_of_string($string) {
return preg_replace('/\x03$/', '', $string);
}
At a guess rtrim($var,"\c"); or preg_replace("/\c+\$/",'',$var);

Categories