I've tried the following:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'car') !== false) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
However if the url contains the word "care" or "car2" it will also trigger "Car Exists". How do I get a match if only "car" is found?
Change
if (strpos($url,'car') !== false) {
into
if (preg_match('/\bcar\b', $url) !== 0) {
Basically, you search for word car with no other alphanumeric surrounding it.
Hope this helps!
Related
I'm using Cakephp 2.x and I am trying to redirect URL's which contain uppercase to lowercase.
For providing more info about the technical part , it is a dictionary which takes a parameter (letter or a word) and then makes a search on the database and in relation of the user's search, it displays a certain result.
What have I done so far is this piece of code in order to be able to redirect those URL's which the user, let's say, by his/her own judgement or mistake inputs letters in uppercase.
In this case, directly when there is an uppercase letter inserted, the result or word must be redirected automatically to lowercase.
´´´
public function comprobar_param($param, $letter = NULL) {
$param_is_letter = false;
$param_is_capital_letter = false;
$categorias_diccionario = 'abcdefghijklmnopqrstuvwxyz';
if(strlen($param) > 0) {
// If it is a lowercase letter
$pos_lowercase = strpos($categorias_diccionario, $param);
$pos_lowercase !== false ? true : false;
$param_is_letter = $pos_lowercase;
$pos_uppercase = preg_match("/[A-Z]/", $param);
//if the letter is a capital letter
$param_is_capital_letter = !$param_is_letter && $pos_uppercase;
}
if(empty($this->request->query)){
$params_url_str = '';
} else {
$params_url_str = '?' . http_build_query($this->request->query);
}
if($param_is_capital_letter === true) {
$this->redirect('/' . DICCIONARIO_MEDICO_URL . '/' . strtolower($param). $params_url_str, 301);
} elseif($param_is_letter === false) {
if($letter != NULL) {
$this->redirect('/' . DICCIONARIO_MEDICO_URL . '/' . $param . $params_url_str, 301);
} else {
$this->cacheAction = array(
'comprobar_param' => '1 year'
);
$this->view(null, $param);
$this->render('view');
}
// if we are on the list with lowercase
} else {
// if the url doesn't end in slash, we redirect the URL with slash at the end
if(substr($this->params->url, -1) != '/') {
$this->redirect('/' . DICCIONARIO_MEDICO_URL . '/' . strtolower($param) . '/' . $params_url_str, 301);
return;
}
}
´´´
Therefore, the route I have been using inside my routes.php file would be the next:
Router::connect('/'.DICCIONARIO_MEDICO_URL . '/:param', array('controller' => 'treatments', 'action' => 'comprobar_param'), array('pass' => array('param' => '')));
At this point, I had the possibility to do the redirect on my local server, but when I am trying to test it on my development server, the redirect function is failing.
I am going to point out an example so you can have a glimpse of what I want to achieve:
http://topdoctors.local/diccionario-medico/Aborto => http://topdoctors.local/diccionario-medico/Aborto
Any suggestion would be welcoming.
Thanks you.
I have a text file containing
number, zero, one
number, two, three
number, four, five
in a file called data.txt
I want to search for the number and tried this but doesnt seem to work
$file = 'domain.com/data.txt';
$searchnum = 'zero';
if (stripos($file, $searchnum) == true) { echo 'number found' }
Update 1.0
i tried this as well but it doesnt seem to pull the data on the txt file
$file = "domain.com/data.txt";
$searchnum = "zero";
if(exec('grep '.escapeshellarg($searchnum).' '.$file )) {
echo "record found";
}
else {
echo "record notfound";
}
You're doing it correctly, you just need to pull the file contents.
$file = file_get_contents('./data.txt'); // you can use a full http address if your server allows it
$searchnum = 'zero';
if (stripos($file, $searchnum) !== false) { echo 'number found'; }
stripos() returns the index of the searched string, or FALSE if it is not found.
So you would do if (stripos($file, $searchnum) !== false) { echo 'number found'; }
!== is used because you need to distinguish false from 0.
I'm trying to check whether the entered text is URL or string. My goal is i should get "URL Detected" if entered text is www.yout.com,http://stackoverflow.com, etc
if (stripos($text, ".com") !== false) {
echo "URL Detected";
}
you mast use filter_var
$incomingData = "www.google.com";
$url = filter_var($incomingData, FILTER_VALIDATE_URL);
if ($url !== false) {
echo "URL Detected";
}
Use regular expression to check if it's an url.
For example :
$regex = "/[-a-zA-Z0-9#:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9#:%_\+.~#?&//=]*)?/"
if(preg_match( $regex , "www.google.com" ){
echo "URL Detected"
}
I've tried the following:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'austin') !== false) {
echo 'Austin metro';
} else if (strpos($url,'georgetown') !== false) {
echo 'Georgetown';
}
The issue is that this will match a URL that has austin anywhere.
For example, the url for georgetown parameter is like this: www.domain.com/austin/georgetown/
So if url has austin/georgetown, would like to display georgetown option.
Any suggestions? Thanks!
Go like this:
$url = 'www.domain.com/austin/georgetown'; //output: georgetown
if (strpos($url,'georgetown') !== false && strpos($url,'austin') !== false)
{
echo 'Georgetown';
}
else if (strpos($url,'georgetown') !== false)
{
echo 'Georgetown';
}
else if (strpos($url,'austin') !== false)
{
echo 'austin';
}
first if condition is checking if austin and georgetown, both are there in the url, if yes georgetown will be printed. rest two conditions are just checking for austin and georgetown individually.
See hear Php fiddle -> https://eval.in/715874
Hope this helps.
I would like to know if some word is present in the URL.
For example, if word car is in the URL, like www.domain.com/car/ or www.domain.com/car/audi/ it would echo 'car is exist' and if there's nothing it would echo 'no cars'.
Try something like this. The first row builds your URL and the rest check if it contains the word "car".
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'car') !== false) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
I think the easiest way is:
if (strpos($_SERVER['REQUEST_URI'], "car") !== false){
// car found
}
$url = " www.domain.com/car/audi/";
if (strpos($url, "car")!==false){
echo "Car here";
}
else {
echo "No car here :(";
}
See strpos manual
if( strpos( $url, $word ) !== false ) {
// Do something
}
worked for me with php
if(strpos($_SERVER['REQUEST_URI'], 'shop.php') !== false){
echo 'url contains shop';
}
This worked for me:
// Check if URL contains the word "car" or "CAR"
if (stripos($_SERVER['REQUEST_URI'], 'car' )!==false){
echo "Car here";
} else {
echo "No car here";
}
If you want to use HTML in the echo, be sure to use ' ' instead of " ".
I use this code to show an alert on my webpage https://geaskb.nl/
where the URL contains the word "Omnik"
but hide the alert on pages that do not contain the word "Omnik" in the URL.
Explanation stripos : https://www.php.net/manual/en/function.stripos
strstr didn't exist back then?
if(strstr($_SERVER['REQUEST_URI'], "car")) {
echo "car found";
}
This must be one of the easiest methods right?
Have a look at the strpos function:
if(false !== strpos($url,'car')) {
echo 'Car exists!';
}
else {
echo 'No cars.';
}
Surely this is the correct way round....
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (!strpos($url,'mysql')) {
echo 'No mysql.'; //swapped with other echo statement
} else {
echo 'Mysql exists.';
}
Otherwise its reporting the opposite way it should...
Starting with PHP 8 (2020-11-24), you can use str_contains:
if (str_contains('www.domain.com/car/', 'car')) {
echo 'car is exist';
} else {
echo 'no cars';
}
You can try an .htaccess method similar to the concept of how wordpress works.
Reference: http://monkeytooth.net/2010/12/htaccess-php-how-to-wordpress-slugs/
But I'm not sure if thats what your looking for exactly per say..
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (!strpos($url,'car')) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
This seems to work.