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.
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 .
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.
Yes, we can use a switch statement with Strings in Java.
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.
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; }
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;
}
}
You can use strpos function as:
if(strpos($_GET['kw'],'berlingo') !== false) {
include 'berlingo.php';
}
if(strpos($_GET['kw'],'c4') !== false) {
include 'c4.php';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With