How to define a exactly match using PHP preg_replace() - php

I am trying to remove some parameters from a URL using PHP preg_replace(). For example, i need to remove a[]=1 from the bellow URL.
$my_url = 'www.myhost.com/filter.php?a[]=1&a[]=12&a[]=13&a[]=14'
So i am using:
$without_filter = preg_replace("/(&)?a\[\]=1/", '', $my_url);
I want to remove only a[]=1, but it is removing the portion that contains a[]=1 from the others parameters, so am i getting:
www.myhost.com/filter.php?234
Someone can help me to solve this?

What about: /a\[\]=1(&|\b)/
That way it will capture a[]=1 only if it is followed by a & or end of string.

Following the man page of preg_replace you may do something like this:
$without_filter = preg_replace("/\&(a\[\]=1)(\&|$)/", '\2', $my_url);
Or... you can always use preg_replace_callback

Use the $limit parameter of preg_replace and set it to 1, this should replace it only once. Assuming that your parameters are always sorted this way:
$without_filter = preg_replace("/(&)?a\[\]=1/", '', $my_url, 1);

Related

Remove end of URL with variable PHP Regex

I suuuuuck at regex and can't even begin to figure out how to remove everything from #edit to the end which contains a veriable of the url from this kind of URL:
https://docs.google.com/presentation/d/1aa_xpsyJtslFJsg4UndsjDvlCe7Vu97_i6Q8zSKofy4/edit?usp=sharing
Any help would be greatly appreciated!
Thanks!
Using strstr() with the third parameter set to true will be the cleanest, most direct non-regex approach. ...and you won't have to sweat your "suuuuucky" regex skills ;) This will isolate the substring from start of the string to the character before your search substring.
Code: (Demo)
$url = 'https://docs.google.com/presentation/d/1aa_xpsyJtslFJsg4UndsjDvlCe7Vu97_i6Q8zSKofy4/edit?usp=sharing';
echo strstr($url, '/edit', true); // https://docs.google.com/presentation/d/1aa_xpsyJtslFJsg4UndsjDvlCe7Vu97_i6Q8zSKofy4
echo "\n";
echo strstr($url, '/edit?', true); // https://docs.google.com/presentation/d/1aa_xpsyJtslFJsg4UndsjDvlCe7Vu97_i6Q8zSKofy4
*note: If the querystring (beginning witih ?) will always exist after /edit, adding the ? to the search substring can only improve accuracy.
Why is this the best function to call? It doesn't leverage the overhead of calling the regex engine, it doesn't generate any temporary arrays, and it is a single function call as opposed to substr()-strrpos().
If your use cases are a bit more complex and this approach is letting you down, calling parse_url() should stabilize things sufficiently to allow you extract the appropriate url components.
Code: (Demo)
$url = 'https://docs.google.com/presentation/d/1aa_xpsyJtslFJsg4UndsjDvlCe7Vu97_i6Q8zSKofy4/edit?usp=sharing';
$components = parse_url($url);
echo $components['scheme'], '://', $components['host'], strstr($components['path'],'/edit',true);
I believe you are trying to parse the query parameters at the end of the url. You can do so by using the explode function:
$url = "https://docs.google.com/presentation/d/1aa_xpsyJtslFJsg4UndsjDvlCe7Vu97_i6Q8zSKofy4/edit?usp=sharing";
print(explode('/edit', $url)[1]);
which will print
?usp=sharing

Replace anchor text with PHP (and regular expression)

I have a string that contains a lot of links and I would like to adjust them before they are printed to screen:
I have something like the following:
replace_this
and would like to end up with something like this
replace this
Normally I would just use something like:
echo str_replace("_"," ",$url);
In in this case I can't do that as the URL contains underscores so it breaks my links, the thought was that I could use regular expression to get around this.
Any ideas?
Here's the regex: <a(.+?)>.+?<\/a>.
What I'm doing is preserving the important dynamic stuff within the anchor tag, and and replacing it with the following function:
preg_replace('/<a(.+?)>.+?<\/a>/i',"<a$1>REPLACE</a>",$url);
This will cover most cases, but I suggest you review to make sure that nothing unexpected was missed or changed.
pattern = "/_(?=[^>]*<)/";
preg_replace($pattern,"",$url);
You can use this regular expression
(>(.*)<\s*/)
along with preg_replace_callback .
EDIT :
$replaced_text = preg_replace_callback('~(>(.*)<\s*/)~g','uscore_replace', $text);
function uscore_replace($matches){
return str_replace('_','',$matches[1]); //try this with 1 as index if it fails try 0, I am not entirely sure
}

replace url using preg_replace php

Hi all i know preg_replace can be used for formatting string but
i need help in that concerned area
my url will be like this
www.example.com/en/index.php
or
www.example.com/fr/index.php
what i want is to get
result as
www.example.com/index.php
i need it in php code so as to set in a session
can anyone please explain how ?
preg_replace('/www.example.com\/(.+)\/index.php/i', "www.example.com/index.php?lang=$1", $url); will do the thing
This is one way to do it:-
$newurl = preg_replace('/\/[a-z][a-z]\//', '/', $url);
Note that the search string appears with quotes and forward slashes ('/.../') and that the forward slashes in the URL then have to be escaped (\/). The language code is then matched with '[a-z][a-z]', but there are several other ways to do this and you may want something more liberal in case there are ever 3 letter codes, or caps. Equally you may need to do something tighter depending on what other URL schemes might appear.
I suspect in this instance it would be faster simply to use str_replace as follows:
$cleanedData = str_replace(array('www.example.com/en/', 'www.example.com/fr/'), '', $sourceData);
Finally i got a method my thanks to Purpletoucan
$newurl = preg_replace('/\/(en|esp|fr)\//', '/', $url);
it's working now i think!

How do I remove a "&" symbol from a URL using regular expressions?

how to remove a & Symbol from a url address use php regular?
for example:http://www.google.com/search?hl=en&q=php
left the & and get :http://www.google.com/search?hl=enq=php
Thanks
$url = 'http://www.google.com/search?hl=en&q=php';
$url = str_replace('&', '', $url);
I'm not sure I really understand the question. It sounds like you just want to remove & characters. That can be easily done:
$url = str_replace('&', '', $url);
You can remove it easily enough with str_replace. Why you would want to do this, however, is another matter entirely.
Are you trying to insert a URL into a page with PHP and getting validation errors due to the & symbol? In that case urlencode() might be what you really need.

regex to get current page or directory name?

I am trying to get the page or last directory name from a url
for example if the url is: http://www.example.com/dir/ i want it to return dir or if the passed url is http://www.example.com/page.php I want it to return page Notice I do not want the trailing slash or file extension.
I tried this:
$regex = "/.*\.(com|gov|org|net|mil|edu)/([a-z_\-]+).*/i";
$name = strtolower(preg_replace($regex,"$2",$url));
I ran this regex in PHP and it returned nothing. (however I tested the same regex in ActionScript and it worked!)
So what am I doing wrong here, how do I get what I want?
Thanks!!!
Don't use / as the regex delimiter if it also contains slashes. Try this:
$regex = "#^.*\.(com|gov|org|net|mil|edu)/([a-z_\-]+).*$#i";
You may try tho escape the "/" in the middle. That simply closes your regex. So this may work:
$regex = "/.*\.(com|gov|org|net|mil|edu)\/([a-z_\-]+).*/i";
You may also make the regex somewhat more general, but that's another problem.
You can use this
array_pop(explode('/', $url));
Then apply a simple regex to remove any file extension
Assuming you want to match the entire address after the domain portion:
$regex = "%://[^/]+/([^?#]+)%i";
The above assumes a URL of the format extension://domainpart/everythingelse.
Then again, it seems that the problem here isn't that your RegEx isn't powerful enough, just mistyped (closing delimiter in the middle of the string). I'll leave this up for posterity, but I strongly recommend you check out PHP's parse_url() method.
This should adequately deliver:
substr($s = basename($_SERVER['REQUEST_URI']), 0, strrpos($s,'.') ?: strlen($s))
But this is better:
preg_replace('/[#\.\?].*/','',basename($path));
Although, your example is short, so I cannot tell if you want to preserve the entire path or just the last element of it. The preceding example will only preserve the last piece, but this should save the whole path while being generic enough to work with just about anything that can be thrown at you:
preg_replace('~(?:/$|[#\.\?].*)~','',substr(parse_url($path, PHP_URL_PATH),1));
As much as I personally love using regular expressions, more 'crude' (for want of a better word) string functions might be a good alternative for you. The snippet below uses sscanf to parse the path part of the URL for the first bunch of letters.
$url = "http://www.example.com/page.php";
$path = parse_url($url, PHP_URL_PATH);
sscanf($path, '/%[a-z]', $part);
// $part = "page";
This expression:
(?<=^[^:]+://[^.]+(?:\.[^.]+)*/)[^/]*(?=\.[^.]+$|/$)
Gives the following results:
http://www.example.com/dir/ dir
http://www.example.com/foo/dir/ dir
http://www.example.com/page.php page
http://www.example.com/foo/page.php page
Apologies in advance if this is not valid PHP regex - I tested it using RegexBuddy.
Save yourself the regular expression and make PHP's other functions feel more loved.
$url = "http://www.example.com/page.php";
$filename = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME);
Warning: for PHP 5.2 and up.

Categories