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";
}
Related
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";
}
i have a variable and i want to use php to check if it contains a group of characters
i would like the code to be like this
$groupofcharacters = ["$","#","*","("];
if($variable contains any of the letters in $groupofcharacters){
//do something}
i know that this will need the use of strpos() function but how can i use the strpos function to check if a variable contains a group of characters without me having to create a strpos() function for all the characters that i want to check for.
please if you don't understand you can tell me in the comments
Best way to solve your issue is by using RegEx. Try this:
<?php
$variable = 'Any string containing $*(#';
$sPattern = '/[$#*(]/';
if (preg_match($sPattern, $variable)) {
// Do something
}
You can use strpbrk to achieve this. The doc says:
strpbrk — Search a string for any of a set of characters
Returns a string starting from the character found, or FALSE if it is not found.
Snippet:
<?php
if(strpbrk($variable,"$#*(") !== false){
// your logic goes here
}
check if it has any char. Using strpos
First you need to combine the array as string after check ot if there's one of them in the
$groupofcharacters = ["$","#","*","("];
$strs = implode("", $groupofcharacters);
foreach(str_split($variable) as $s) {
if (strpos($s, $strs)) {
echo "it contains "; continue;
}
}
I need to check if string contains two or more specific words if so echo words found..i try using this..but if one word is found then it echo found and it now works:
Try code:
if(preg_match("/(s|g898|w63)/i", $string)){
echo 'found';
}
So i try using this example:
$string = 'Today is wonderfull day w63';
Function above need to do nothing.
$string = 'Above the sky limit s g898 w63';
This string need to echo from above function echo 'found'.
So how to check in PHP if string containts all three specific word and if contains all three echo found?
that is one possible solution
if(preg_match("/(?=.* s )(?=.*g898)(?=.*w63)/i", $string)){
echo 'found';
}
I am trying to find if a persons name contains any numbers or symbols. I am not very sure how to use preg_match and all the examples online make no sense can someone please explain how i can check if a value has numbers or symbols. And if you can please explain how it works.
To check if the person name contains only number. The code below has been tested and is working
<?php
error_reporting(0);
$number = '001222288';
if (!preg_match('/^[0-9]*$/', $number)) {
//error
echo 'Error does not contain only number';
} else {
echo 'success. it contains only number';
}
?>
if the variable number contains any other thing that is not number it result in error. eg
$number = 'ABFRT001222288';
To check for alphabets or string
<?php
error_reporting(0);
$string = 'ABTYUUU';
if (!preg_match('/^[A-Z]*$/', $string)) {
//error
echo 'Error does not contain only alphabet';
} else {
echo 'success. it contains only alphabets';
}
?>
Mark this as correct answer if it solve your problem
Thanks
The function preg_match use regex expressions. You can use an online tool for test your regex. I use debuggex, preg_match use PCRE expressions. In your case you should, you may use for numbers
preg_match("/[0-9]+/", $subject);
EDIT : We need more details for the symbols you want select
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);