Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a PHP switch statement to check if a string contains a word (but can also contain others)?

I'm using a PHP switch to include certain files based on the incoming keywords passed in a parameter of the page's URL.

The URL, for example, could be: ...page.php?kw=citroen%20berlingo%20keywords

Inside the page, I'd like to use something like this:

<?
    switch($_GET['kw']){

        case "berlingo":     
            include 'berlingo.php'
            break;
        case "c4":
            include 'c4.php';
            break;

    } 
?>

What I want to do in the first case is include the berlingo.php file if the keyword parameter contains berlingo, but it doesn't have to be exactly that keyword alone.

For example, I want to include the berlingo.php file if the keyword is berlingo, but also if it's citroen berlingo.

How can I evaluate if a given string contains a value using a PHP case select (switch statement)?

Thanks.

like image 380
sebastian Avatar asked Nov 14 '10 02:11

sebastian


People also ask

How do you check if a string contains a specific word in PHP?

You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How do you see if a string contains a word?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

Can you do a switch statement with strings?

Yes, we can use a switch statement with Strings in Java.

How will you locate a string within a string in PHP?

The strpos() function finds the position of the first occurrence of a string inside another string. Note: The strpos() function is case-sensitive. Note: This function is binary-safe.


3 Answers

Based on this question and this answer, the solutions I've come up with (while still using a case select) are below.

You can use either stristr() or strstr(). The reason I chose to use stristr() in this case is simply because it's case-insensitive, and thus, is more robust.

Example:

$linkKW = $_GET['kw'];  switch (true){    case stristr($linkKW,'berlingo'):       include 'berlingo.php';       break;    case stristr($linkKW,'c4'):       include 'c4.php';       break; } 

You could also use stripos() or strpos() if you'd like (thanks, Fractaliste), though I personally find this more difficult to read. Same deal as the other method above; I went the case-insensitive route.

Example:

$linkKW = $_GET['kw'];  switch (true){    case stripos($linkKW,'berlingo') !== false:       include 'berlingo.php';       break;    case stripos($linkKW,'c4') !== false:       include 'c4.php';       break; } 
like image 91
baacke Avatar answered Sep 23 '22 04:09

baacke


Since in a switch statement only a simple equality testing will be performed it won't help you much here. You need to run the string through a string matching function, best suited of which is strpos. The straight forward answer is:

if (strpos($_GET['kw'], 'berlingo') !== false) {
    include 'berlingo.php';
} else if (strpos($_GET['kw'], 'c4') !== false) {
    include 'c4.php';
} … and so on …

The more elegant solution would be something like this:

$map = array('berlingo' => 'berlingo.php', 'c4' => 'c4.php', …);
foreach ($map as $keyword => $file) {
    if (strpos($_GET['kw'], $keyword) !== false) {
        include $file;
        break;
    }
}

Or, if the correspondence between the keyword and the file is always 1:1:

$keywords = array('berlingo', 'c4', …);
foreach ($keywords as $keyword) {
    if (strpos($_GET['kw'], $keyword) !== false) {
        include "$keyword.php";
        break;
    }
}
like image 23
deceze Avatar answered Sep 19 '22 04:09

deceze


You can use strpos function as:

if(strpos($_GET['kw'],'berlingo') !== false) {
 include 'berlingo.php';
}
if(strpos($_GET['kw'],'c4') !== false) {
 include 'c4.php';
}
like image 36
codaddict Avatar answered Sep 19 '22 04:09

codaddict