How to check if URL contains certain words?

check if url contains query string javascript
check if url contains string javascript
check if url contains http javascript
css if url contains
check if url contains query string jquery
check if url contains string php
check if url has parameters javascript
c# check if url contains string

I want to show custom content if current URL contain certain words.

So far, I'm able to achieve this if the URL contains just the word, 'cart', using the code below. However, I want to be able to check for other words like 'blog', 'event' and 'news'. How do I go about this.

<?php $path = $_SERVER['REQUEST_URI'];
$find = 'cart';
$pos = strpos($path, $find);
if ($pos !== false && strstr($_SERVER['HTTP_REFERER'], 'path/to/site') !== false) : 
            ?>
  Custom content      
<?php else: ?>

Use an array but use preg_grep instead. IMO it's the correct preg_ function for this use case.

preg_grep — Return array entries that match the pattern

 //www.example.com?foo[]=somewords&foo[]=shopping+cart

//for testing
$_GET['foo'] = ['somewords', 'shopping cart'];

$foo = empty($_GET['foo']) ? [] : $_GET['foo'];

$words = ['cart','foo','bar'];

$words = array_map(function($item){
             return preg_quote($item,'/');
        },$words);

$array = preg_grep('/\b('.implode('|', $words).')\b/', $foo);

print_r($array);

Output

Array
(
    [1] => shopping cart
)

Sandbox

Javascript: How to check if URL contains a word, The method is String.prototype.indexOf() if(window.location.href.indexOf(" Desktop") > -1) { alert("Alert: Desktop!"); } and you don't need DOM� 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

Use an Array and loop through it .. IE

<?php $path = $_SERVER['REQUEST_URI'];
$arr = array();
$arr[0] = 'cart';
$arr[1] = 'foo';
$arr[2] = 'bar';

foreach($arr as $find){

   $pos = strpos($path, $find);
   if ($pos !== false && strstr($_SERVER['HTTP_REFERER'], 'path/to/site') !== false){ 
      echo "custom content";
      break; // To exit the loop if custom content is found -- Prevents it showing twice or more
    }      
}

How to check if the URL contains a given string?, You need add href property and check indexOf instead of contains. I'm trying to create a formula to look for specific text. So, I have a list of projects that either contain "internal" or "external" in the project name, let's say project name is in cell A1. I am trying to create a formula to say that if cell A1 contains the text "internal" then return the value "internal" otherwise return "external".

There are several solutions such as preg_match_all():

Code
<?php $path = $_SERVER['REQUEST_URI'];
$find = '/(curt|blog|event|news)/i';
$number_of_words_in_my_path  = preg_match_all($find, $path);
if ($number_of_words_in_my_path > 0 && strstr($_SERVER['HTTP_REFERER'], 'path/to/site') !== false) : 
            ?>
  Custom content
<?php else: ?>

How do I check if URL contains certain words, I want to show a custom content if current URL contain certain words. So far, I'm able to achieve this with the code below (works perfectly), if the� How to Check if the URL contains string in jQuery Use Window.location.href to take the url in javascript. it’s a property that will tell you the current URL location of the browser. I want to check if the URL contains the string “?language=” in jQuery. In below jQuery code we can easily check it:

Check if the URL contains string in jQuery, Count word contain UTF 8 character in javascript � How to check if string contains a specific word in PHP? Disable Past date time in� Check if text contains certain words Hello, If have a calculated dimesion where i want to check on the content of the fields. Example:

check if url contains string Code Example, Get code examples like "check if url contains string" instantly right from your how to get particular word from url in html � jquery check if url contains string � js if � To check if a cell contains specific text, you can use the SEARCH function together with the ISNUMBER function. In the generic version, substring is the specific text you are looking for, and text represents text in the cell you are testing. In the example shown, the formula in D5 is: = ISNUMBER(SEARCH(C5, B5))

if url contains string jquery Code Example, Get code examples like "if url contains string jquery" instantly right from javascript check url � vue router check if url contains a certain word� You can replace any word in the following formula to check if cell contains word then assign value. =IFERROR (IF (SEARCH (“Word”,A2,1)>0,1,0),””) Search function will check for a given word in the required cell and return it’s position.

Comments
  • Thanks @Lawrence for the edit .. I was hasty with the ole copy paste!