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.
Related
I'm trying to write a function that will insert a backlash into the broken URL that for some reason is missing a second backslash after http, and I only want to return fixed version, for example
addSlash(http:/example.net) -> http://example.net
addSlash(https:/example.net`) -> https://example.net
I thought this could be solved with preg_replace in one line of code, but I can't get it to work. Using $url = 'http:/example.net' and
preg_replace("#^(https?:)(.*?)#", "\1/\2", $url);
I'm getting back / /example.net , as if 'http' is not matched and placed into \1.
Any suggestions ? I would like to avoid callbacks and anonymous function if possible, cause this is supposed to run on an older version of PHP.
/^(https:|http:)?[\\/](?![\\/])(.*)/
Something like that should work for you.
$re = "/^(https:|http:)?[\\/](?![\\/])(.*)/mi";
$str = "https:/regex101.com\nhttp:/regex101.com\nhttps://regex101.com";
$result = preg_replace($re, '$1//$2' , $str);
var_dump($result);
This should work:
preg_replace('#^(https*:)(/.*)#', '\1/\2', $url);
Or even:
preg_replace('#^(https*:)#', '\1/', $url);
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);
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!
I'm trying to write a regex in php that in a line like
<a href="mypage.php?(some junk)&p=12345&(other junk)" other link stuff>Text</a>
and it will only return me "p=12345", or even "12345". Note that the (some junk)& and the &(otherjunk) may or may not be present.
Can I do this with one expression, or will I need more than one? I can't seem to work out how to do it in one, which is what I would like if at all possible. I'm also open to other methods of doing this, if you have a suggestion.
Thanks
Perhaps a better tactic over using a regular expressoin in this case is to use parse_url.
You can use that to get the query (what comes after the ? in your URL) and split on the '&' character and then the '=' to put things into a nice dictionary.
Use parse_url and parse_str:
$url = 'mypage.php?(some junk)&p=12345&(other junk)';
$parsed_url = parse_url($url);
parse_str($parsed_url['query'], $parsed_str);
echo $parsed_str['p'];
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.