avatar
check if any string would be existed within given string PHP

• Take advantage of using the strpos() function to check if any string would have existed within a given string.

» PHP

$a = 'Saturday';
$b = 'Tuesday, Thursday, Saturday';

if (strpos($b, $a) !== false) {
    echo "$a exists in $b";
} else {
    echo "$a does not exist in $b";
}

and another way

$a = 'Saturday';
$b = 'Tuesday, Thursday, Saturday';

if (strstr($b, $a)) {
    echo "$a exists in $b";
} else {
    echo "$a does not exist in $b";
}

» Javascript

let a = 'Saturday';
let b = 'Tuesday, Thursday, Saturday';

if (b.includes(a)) {
    console.log(`${a} exists in ${b}`);
} else {
    console.log(`${a} does not exist in ${b}`);
}

and another way

let a = 'Saturday';
let b = 'Tuesday, Thursday, Saturday';

let regex = new RegExp(a, 'i'); // 'i' flag for case-insensitive matching
if (b.match(regex)) {
    console.log(`${a} exists in ${b}`);
} else {
    console.log(`${a} does not exist in ${b}`);
}

» Java

public class Main {
    public static void main(String[] args) {
        String a = "Saturday";
        String b = "Tuesday, Thursday, Saturday";

        if (b.contains(a)) {
            System.out.println(a + " exists in " + b);
        } else {
            System.out.println(a + " does not exist in " + b);
        }
    }
}

and another way

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Main {
    public static void main(String[] args) {
        String a = "Saturday";
        String b = "Tuesday, Thursday, Saturday";

        Pattern pattern = Pattern.compile(a, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(b);

        if (matcher.find()) {
            System.out.println(a + " exists in " + b);
        } else {
            System.out.println(a + " does not exist in " + b);
        }
    }
}

» Python

a = 'Saturday'
b = 'Tuesday, Thursday, Saturday'

if a in b:
    print(f"{a} exists in {b}")
else:
    print(f"{a} does not exist in {b}")
You need to login to do this manipulation!