PHP using STRPOS to find partial value in CLASS - php

I need to search inside a string to find if there ise any matches to this pattern:
class="%men%"
It means that the class may be equal to either:
class="men"
class="mainmen"
class="MyMen"
class="menSuper"
class="MyMenSuper"
etc
I need someting like strpos($string,'class="%men%') where % could be anything.
Best,
Martti

Try using preg_match
if (preg_match("/men/i", $class)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
Look at this link for more information http://www.php.net/preg_match
Edit :
So you can do like this (Inspired from the answer of Marius.C)
$string = '<div class="menlook">Some text</div>';
if (preg_match('/class="(.*)men(.*)"/i', $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}

Store class "men" as string in variable like "$your_class"
then use preg_match like this:
if(preg_match('/men/i', $your_class)) {
echo "Men Class Found!";
}
ref: http://php.net/preg_match
or using strpos:
if(strpos(strtolower($your_class),'men')!==false) {
echo "Men Class Found!";
}
ref: http://www.w3schools.com/php/func_string_strpos.asp

Use strpos two times,
if(strpos($string,'class=') !== false && strpos($string,'men') !== false){
echo "true";
}
Note: strpos is much faster than preg_match.

This is possible without regular expressions which are quite slow
stristr($string, 'men')

I believe that you need something like this:
preg_match_all('/class="(.*)men(.*)"/i', $string, $matches);

Related

How to find a word from a sentence using php case insensitive?

I am having difficulty to find a word from a sentence. Using the code below it should return false but its returning true. Can anyone help me, please?
if(stristr('I live in a farmhouse','farm'))
{
echo 'true';
}
else
{
echo 'false';
}
For this mission a Regular Expression (regexp) would come handy. There is \b separator which means "word boundry". So let's see how it goes:
(The i at the end means insensitive)
if (preg_match('/\bfarm\b/i', 'I live in a farmhouse')) {
echo 'true';
} else {
echo 'false';
}

How to check if string contains part of string (not whole string) in PHP

I have problem with checking if part of string is containing other string but strpos and stripos are not working for me.
The case that I need to check if word pris is containing in string -med prista-
Any suggestions?
Try
$str = '-med prista-';
if (preg_match('/pris/', $str))
echo 'OK';
else
echo 'Not';
In PHP 8 you have a new function called str_contains() created for this purpose:
if (str_contains('-med prista-', 'pris')) {
echo "Found!";
}
If you are using older PHP versions then you can use mb_strpos (multibyte), or strpos functions like this:
if(mb_strpos('-med prista-', 'pris') === false){
// not found
} else {
echo "Found!";
}

How to check/find if multiple letters are in a string | PHP

So far, I managed to find if ONE letter is in ANY given string. However, I cannot manage to ask for multiple conditions like 'a' && 'e' && 'i'... In a way that all of those letter need to be in the string so the echo "Vowel found" gets out.
I have tried with stristr, strpos... and so far my best guess was deprecated so I couldn't use it.
My last resource, which didn't work anyway, was to nest if conditions to make it work; also tried with arrays with no positive result.
$string = 'murcielago'; // a word with 5 vowels
if (stristr($string, 'a') == TRUE) {
echo 'Vowel found';
} else {
echo "Vowel not found";
}
This is my code for ONE string or letter to be found in the original given string.
It made me think further! Got it.
$string = "murcielago";
if (strpos($string,'a') && strpos($string,'e') && strpos($string,'i') && strpos($string,'o') && strpos($string,'u') == true)
{
echo "nailed it";
} else {
echo "No vowels for you";
}
ยดยดยด
I wonder if there is any way to make all the conditions into one since I am still using the same strpos function.
Try using a regex and preg_match
<?php
//Enter your code here, enjoy!
$string = 'murcielago';
// This will echo "vowel found"
if(preg_match( '`[aeiouy]`', $string)){
echo "vowel found\n";
}
$string2 = 'bcdfgplk';
// This won't echo anything
if(preg_match( '`[aeiouy]`', $string2 )){
echo "vowel not found\n";
}

Check if HTML code contains words

How does know how I can make a PHP script that checks if a HTML code stored as a variable contain certain words?
To illustrate my problem:
$html = "<p>My name is Herman</p><br><span>I like to eat hamburgers</span>";
For example; I need a PHP script that can check if this variable contains "Herman" or "hamburger".
Thanks you :)
Use strpos.
<?php
$html = "<p>My name is Herman</p><br><span>I like to eat hamburgers</span>";
if(strpos($html, "Herman") !== false) {
echo "string found";
} else {
echo "string not found";
}
?>
You should probably use function strtolower to make your search case insensitive, then use strpos. It will find the numeric position of the first occurrence of your search in the haystack string return 0 if not found.
$lowhtml=strtolower($html);
if((strpos($lowhtml, 'hamburger')!=false) && (strpos($lowhtml, 'herman')!=false)){
echo "Contains";
}else{
echo "Doesn't contain";
}

PHP - preg_match - How to match a string upper/lower case with anything before or after it?

I have a part of a function that goes like this:
if (preg_match("#\bscript\b#",$userInput))
{
$bannedWord = 'script';
logHax();
return TRUE;
}
This is causing a problem for what I am trying to accomplish because it will only match the exact word "script" and not variations of it, like "ScriPt" or "<script>".
What I would like to have is the examples of the not matched strings along with the original string return true.
How's this:
if (preg_match("/<script\b[^>]*>/i",$userInput))
{
$bannedWord = 'script';
logHax();
return TRUE;
}
Case-insensitive matching:
preg_match("#\bscript\b#i",$userInput)
Note the i. Also note that this the first example in the docs:
<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
Cheers
If you really want to match "anything" before or after the string (not just a word), then you do not even need preg_match here, bacuse you could do something like this:
$userInputLower = strtolower($userInput);
if (strpos($userInputLower, 'script') !== false)
{
$bannedWord = 'script';
logHax();
return TRUE;
}

Categories