Custom Body Class from URI - php

I'm working on how to get a custom class into my body tag. I'd like to parse the URI and drop each part of the URI into the class as a separate string. This is a far as I've gotten. I've got the URI parsed and placed into the body class, but as a single string separated by /'s instead of spaces.
Also, I need the IF statement to add "home" if the URI is simply "/". Unfortunately, my current IF statement blows up the whole page. :-(
<?
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
if $url = '' {
$url = 'home';
}
endif;
?>
<body class="<?=$url?>">

try this
<?
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
$url = str_replace ("/", " ", $url);
if ($url == '') {
$url = 'home';
}
?>
<body class="<?=$url?>">

I suggest you to use regular expressions.
<?php
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
if (preg_match("/^\\/?$/", $url)) {
$url = 'home';
}
endif;
?>
"/^\\/?$/" this is regular expression
^ - means beggining of string
$ - means end of string
\\/ - it's / sign, but it has to be escaped by \\
? means that sign before is optional
It will make match when REQUEST_URI will be empty, or it would contain only / sign.

Related

Internal Server Error when trying to check url

I'm trying to check the string after the last trailing slash in my URL.
My code is as follows:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$data = substr($url, strrpos($url, '/') + 1);
if($data == "dashboard") {
require_once VIEW_ROOT . '/cp/dashboard_view.php';
} else {
echo $data;
}
Once I go to http://MYURL/dashboard/in it should show in as the $data. Instead it gives me a 500 error.
You can simply use explode() function to break the string... .Or else $_SERVER[REQUEST_URI] shall give you the data after the host name...
But for the data after the last '/' explode function will work the best..
This will work.
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$x = explode('/',$url);
$data = $x[sizeof($x)-1];
echo $data;
You should try :
$url = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
You need to join
http:// string with $_SERVER[HTTP_HOST] and then $_SERVER[REQUEST_URI] using .(dot).

URL cutsoff at special characters when doing GET in PHP

I have this domain
https://test.com/?url=https://google.com/search?q=#ie7&rls=login.microsoft:en-US:IE-Address&ie=&oe=#
And this is the code:
<?php
//check if the url parameter exists
if(isset($_GET['url'])) $url=$_GET['url'];
else $url=FALSE;
?>
If I use this in the html
<?=(!$url) ? '' : $url ?>
I get an output that cuts off in special characters like # or & etc.. and becomes like this for example https://google.com/search?q=# I tried urlencode/decode but couldn't figure it out
if you need all after ?url=, you can try this:
$url = $_SERVER['REQUEST_URI'];
$url2 = substr($url, strpos($url, '?url')+5);
$url = substr($url, strpos($url, '?url'));
print($url); // url?=https://..etc
print($url2); // https://..etc
you want to output the full domain just after
https://test.com/?url=
to the end, and get https://google.com/search?q=#ie7&rls=login.microsoft:en-US:IE-Address&ie=&oe=# right?
So if this is fixed, you can do:
echo substr($url, 22);

Add http:// to an url that have double slash at the beginning (or nothing)

For instance, I need to add http:// to submitted url like :
//www.mydomain.com/images/icon-32.png (double slash at the beginning)
or
www.mydomain.com/images/icon-32.png
Any idea?
This seems to work fine for your problem.
$url = "//mydomain.com/images/icon-32.png";
// or $url = "mydomain.com/images/icon-32.png";
// Checking if string starts with '//'
if(strpos($url, "//")===0){
$url = "http:" . $url;
}else{
// Checking if string doesn't already start with 'http://'
if(!strpos($url, "http://")===0 || strpos($url, "http://")===false)
$url = "http://" . $url;
}
echo $url;

Remove the last character using strrchr and substr in PHP

User may enter abc.com/ instead of abc.com, so I want to do validation using strchr.
This works but looks strange
if(strrchr($url, "/") == "/"){
$url = substr($url, 0,-1);
echo $url;
}
Is there a better way of doing this?
Yes - using the optional second argument to trim() or rtrim() you can specify a character list to trim off the end of a string.:
$url = rtrim($url, '/');
If the trailing / is present, it will be stripped and the string returned without it. If not present, the string will be returned in its original form.
// URL with trailing /
$url1 = 'example.com/';
echo rtrim($url1, '/');
// example.com
// URL without trailing
$url2 = 'example.com';
echo rtrim($url2, '/');
// example.com
// URL with many trailing /
$url3 = 'example.com/////';
echo rtrim($url3, '/');
// example.com
Add additional characters into the string with '/' if you want to strip whitespace as well, as in rtrim($url, ' /')
If you merely want to test if the last character of the URL was /, you may do so with substr()
if (substr($url, -1) === '/') {
// true
}

Codeigniter and preg_replace

I use Codeigniter to create a multilingual website and everything works fine, but when I try to use the "alternative languages helper" by Luis I've got a problem. This helper uses a regular expression to replace the current language with the new one:
$new_uri = preg_replace('/^'.$actual_lang.'/', $lang, $uri);
The problem is that I have a URL like this: http://www.example.com/en/language/english/ and I want to replace only the first "en" without changing the word "english". I tried to use the limit for preg_replace:
$new_uri = preg_replace('/^'.$actual_lang.'/', $lang, $uri, 1);
but this doesn't work for me. Any ideas?
You could do something like this:
$regex = '#^'.preg_quote($actual_lang, '#').'(?=/|$)#';
$new_uri = preg_replace($regex, $lang, $uri);
The last capture pattern basically means "only match if the next character is a forward slash or the end of the string"...
Edit:
If the code you always want to replace is at the beginning of the path, you could always do:
if (stripos($url, $actual_lang) !== false) {
if (strpos($url, '://') !== false) {
$path = parse_url($url, PHP_URL_PATH);
} else {
$path = $url;
}
list($language, $rest) = explode('/', $path, 2);
if ($language == $actual_lang) {
$url = str_replace($path, $lang . '/' . $rest, $url);
}
}
It's a bit more code, but it should be fairly robust. You could always build a class to do this for you (by parsing, replacing and then rebuilding the URL)...
If you know what the beginning of the URL will always, be, use it in the regex!
$domain = "http://www.example.com/"
$regex = '#(?<=^' . preg_quote($domain, '#') . ')' . preg_quote($actual_lang, '#') . '\b#';
$new_uri = preg_replace($regex, $lang, $uri);
In the case of your example, the regular expression would become #(?<=^http://www.example.com/)en\b which would match en only if it followed the specified beginning of a domain ((?<=...) in a regular expression specifies a positive lookbehind) and is followed by a word boundary (so english wouldn't match).

Categories