replace url using preg_replace php - 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!

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

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.

PHP Regex help, getting part of a link

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'];

php: delete elements from string

I have this sitation:
..<img src="//http://www... OR ..<img src="/http://www... OR ..<img src="////http://www...
(/ - may be much)
How delete / before http?
Resultat always should be:
..<img src="http://www...
Thanks ;)
This should do the trick.
ltrim($url, "/");
This seems like a rather ad hoc solution. You might want to get to the bottom of the issue and eliminate it at source.
A regular expression along the lines of this should do the trick I think:
$string = preg_replace('/="\/+http:/', '="http:', $string);
Assuming that the url is defined in a variable within your PHP, ltrim() could be the answer
$url = ltrim($url,'/');
though you wouldn't be able to use this option if you had local url's (eg '/images/img.gif') without the 'http://'
You could do something like this (str_replace() because it is faster than a regular expression):
$markup = str_replace('//http://', 'http://', $markup);
Why do you need this? It might be better to eliminate the source of this problem.

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