how to define end of string - php

I want to add "/info" in the end of urls. If the "/info" already exists, I would like to leave as it is.
I'm currently using:
if(strpos($url, "/info") === false){
$url .= "/info";
}
But the above code works only if the url doesn't contain "/" at the end.
For example: if the url is http://www.domain.com then it works perfectly and the output is http://www.domain.com/info. If the url is http://www.domain.com/ then it shows http://www.domain.com//info.
How to avoid this?

Trim the domain, then check the last five characters (in case the "/info" string appears elsewhere in the URL).
$url = rtrim($url,'/');
if(substr($url,-5)!='/info') $url .= '/info';

You just need to take that into consideration.
if(substr($url, -5) != '/info') {
if(substr($url, -1) == "/")
$url.="info";
else
$url.="/info";
}
Note I modified the first if to only check for '/info' at the end of the url; as Gareth did ;)

consider following url :
http://www.domain.com/info/test/
if you use strpos in that way , you will get wrong result.
instead you can use substr and rtrim :
$url = (substr($url,-5) != '/info') ? rtrim($url, "/") . '/info' : $url;

Related

PHP regex: How to remove ?file in url?

My url like this:
http://mywebsite.com/movies/937-lan-kwai-fong-2?file=Rae-Ingram&q=
http://mywebsite.com/movies/937-big-daddy?file=something&q=
I want to get "lan-kwai-fong-2" and "big-daddy", so I use this code but it doesn't work. Please help me fix it ! If you can shorten it, it is so great !
$url= $_SERVER['REQUEST_URI'];
preg_replace('/\?file.*/','',$url);
preg_match('/[a-z][\w\-]+$/',$url,$matches);
$matches= str_replace("-"," ",$matches[0]);
First there are issue with your code which im going to go over because they are general things:
preg_replace does not work by reference so you are never actually modifying the url. You need to assign the result of the replace to a variable:
// this would ovewrite the current value of url with the replaced value
$url = preg_replace('/\?file.*/','',$url);
It is possible that preg_match will not find anything so you need to test the result
// it should also be noted that sometimes you may need a more exact test here
// because it can return false (if theres an error) or 0 (if there is no match)
if (preg_match('/[a-z][\w\-]+$/',$url,$matches)) {
// do stuff
}
Now with that out of the way you are making this more difficult than it needs to be. There are specific function for working with urls parse_url and parse_str.
You can use these to easily work with the information:
$urlInfo = parse_url($_SERVER['REQUEST_URI']);
$movie = basename($urlInfo['path']); // yields 937-the-movie-title
Just replace
preg_replace('/\?file.*/','',$url);
with
$url= preg_replace('/\?file.*/','',$url);
Regex works, and parse_url is the right way to do it. But for something quick and dirty I would usually use explode. I think it's clearer.
#list($path, $query) = explode("?", $url, 2); // separate path from query
$match = array_pop(explode("/", $path)); // get last part of path
How about this:
$url = $_SERVER['REQUEST_URI'];
preg_match('/\/[^-]+-([^?]+)\?/', $url, $matches);
$str = isset($matches[1]) ? $matches[1] : false;`
match last '/'
match anything besides '-' until '-'
capture anything besides '?' until (not including) '?'

Match URL link and ignore the page behind

I have this code that will match the url and than do something, its working good but when the url has some page behind like /board/users/somename/replies/page/2/ the code will not work. How can I only detect this part of url /board/users/somename/replies/ and ignore the rest behind? Thanks for helping.
$pageURL = $_SERVER['REQUEST_URI'];
$myurl = '/board/users/somename/replies/';
if (strtolower($pageURL) == strtolower($myurl) ) {
echo "right";
}else{
echo "wrong";
}
You just need to match the start of the string, like this:
if ( strpos(strtolower($pageURL), strtolower($myurl)) === 0 )
You also have to ensure that the leading and trailing slashes match up, so I would suggest you modify the first two lines of your code like this:
$pageURL = '/' . trim($_SERVER['REQUEST_URI'], '/');
$myurl = '/board/users/somename/replies';
EDIT: I had got the slashes wrong. That's fixed now.
simply try a regular expression
if preg_match('/'.$myurl.'/', $pageUrl) {}
this will match any url that has $myurl in it. preg_match returns 1 if the regular expression matches, and 0 if it doesn't.
if you want to exclude the URL's with page/ in it, try a negative lookahead via regex
if preg_match('/(?!'.$myurl.'page\/)'.$myurl.'/', $pageUrl) {}
The negative lookahead makes sure myurl/page/ is not in the URL and then you simply match for your url.
edit: for further information on preg_match check the php manual at preg_match
You can also use PHP stristr function to match the beggining of a string. (case-insensitive)
Try this:
$pageURL = $_SERVER['REQUEST_URI'];
$myurl = '/board/users/somename/replies/';
if (stristr($pageURL, $myurl) !== FALSE) {
echo "right";
} else {
echo "wrong";
}
I hope it helps!

PHP : Cropping a selected text from a string

I want to get the string part of a URL cropping off http:// like from http://google.com I need to crop http:// and get google.com.
I used the following code an it gives me /google.com
strrchr("http://google.com" , "//");
how can I do this? ow can I get only google.com
*Update: * Google.com is just an example, the url can be a long url like http://artile.blogspot.com/article.htm so i need article.blogspot.com/article.htm
The function parse_url() is what you're looking for.
As Lawrence says, the exact code will be:
$host = parse_url('http://google.com',PHP_URL_HOST);
An alternative would be str_replace()
$host = str_replace("http://", "", "http://google.com", 1);
The fourth parameter(count) makes sure that it'll only replace the first instance of http://
Why not just look to see if the string starts with http:// and then use a function to get the remaining sub-string?
$url = 'http://google.com';
if (strpos($url, 'http://') === 0) {
$url = substr($url, 7);
}
If there are other prefixes that you would like to remove, then perhaps it might be time to start looking into a quick regex to get the job done. For example:
$url = 'https://google.com';
$url = preg_replace('#^(?:https?|ftps?|news|feed|gopher)://#', '', $url);
you could also use [^http://]+$

Remove urls that have certain domain in them

I have a set of urls for example
http://t3.gstatic.com/images?q=tbn:ANd9GcRfLZhH0jpyUJxGtsiHcldUPiNQsosLdR9xgcYqVWyRWGYS4qtt
http://feeds.feedburner.com/~r/DrudgeReportFeed/~4/zSLWG4ybmjw
I want to remove any url that has feeds.feedburner.com in it. What regular expression would I use? (php)
Why use regex? Use parse_url.
$urlData = parse_url($url);
if ($urlData['host'] != 'feeds.feedburner.com'){
// Not a feedburner url
}
Shorthand, by the way, is as follows:
if (parse_url($url, PHP_URL_HOST) != 'feeds.feedburner.com'){
// same outcome
}
Use this regexp:
/feeds\.feedburner\.com/

How to determine if URL ends in /site-map

I need determine if the current URL ends in /site-map
For example: site.com/site-map
Or
site.com/somedirectory/site-map
Is there a PHP method to pull this value?
You could use substr to check the last 9 characters:
$url = $_SERVER['REQUEST_URI'];
if (substr($url,-9)=="/site-map")
edit to accommodate the url ending with /site-map/ occasionally you could do this:
$url = $_SERVER['REQUEST_URI'];
if (substr($url,-9)=="/site-map" || substr($url,-10)=="/site-map/")
Here's a preg_match() solution. Likely to be a bit slower than strpos() & substr(), but more flexible.
$url = $_SERVER['REQUEST_URI'];
if (preg_match("/\/site-map$/", $url)) {
// it ends in /site-map
}

Categories