I am searching through text line by line and want to see if the line contains the phrase "see details" and is not case sensitive, so will find:
See Details, See details, SEE Details etc
I have this so far.
if(preg_match("/^(\see details)/", strtolower($line)))
{
echo 'SEE DETAILS FOUND';
}
A simple example would be helpful thanks.
If you want to check if a sub-string is present in a string, no need for regular expressions : stripos() will do just fine :
if (stripos(strtolower($line), 'see details') !== false) {
// 'see details' is in the $line
}
stripos() will return the position of the first occurrence of the sub-string in the string ; or false if the sub-string is not found.
Which means that if it returns something else than false, the sub-string is found.
Your regex is actually broken.
/^(\see details)/
This breaks down into:
At the beginning
Open a capturing group
Look for one whitespace character
Followed by all of the following characters: ee details
Close the group
\s is an escape sequence matching whitespace. You could also have added the i modifier to make the regex case-insensitive. You also don't seem to be doing anything with the captured group, so you can ditch that.
Therefore:
/^see details/i
is what you'd want.
You mentioned that you're going through input line by line. If you only need to know that the entire input contains the specific string, and you have the input as a string, you can use the m modifier to make ^ match "beginning of a line" instead of / in addition to "beginning of the string":
/^see details/im
If this is the case, then you would end up with:
if(preg_match('/^see details/im', $whole_input)) {
echo "See Details Found!";
}
But as others have mentioned, a regex isn't needed here. You can (and should) do the job with the more simple stripos.
As Pascal said, you can use stripos() function although correct code would be:
if (stripos(strtolower($line), 'see details') !== false) {
// 'see details' is in the $line
}
accoding to the php documentation (http://www.php.net/manual/en/function.preg-match.php):
<?php
/* The \b in the pattern indicates a word boundary, so only the distinct
* word "web" is matched, and not a word partial like "webbing" or "cobweb" */
if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
if (preg_match("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
which it looks pretty simple and nice :-).
Related
There have been several questions on this but none of them have lead me to the answer, they're all too specific or too long and complex. Mine is more general and simple. I have a pattern like this:
/you(?:.*)? see/
I want it to match all these sentences except the last:
you can see
you could see
you see
you could not see
At the moment it matches everything but it also matches the last sentence. I need it to only match any one single word or no word. I also tried this pattern but it doesn't quite work either:
/you(?:[^\s]+)? see/
This one does the job:
/^you(?:\s+\w+)?\s+see$/
Where (?:\s+\w+)? is an optional non capture group that matches 1 or more spaces followed by 1 or more word character (ie. [a-zA-Z-9_])
In action:
$strings = array(
'you can see',
'you could see',
'you see',
'you could not see',
);
foreach ($strings as $str) {
if (preg_match('/^you(?:\s+\w+)?\s+see$/', $str)) {
echo "$str : matches\n";
} else {
echo "$str : doesn't match\n";
}
}
Output:
you can see : matches
you could see : matches
you see : matches
you could not see : doesn't match
I would have deleted the question since I solved it shortly afterwards but there was already a response. I found just changing the pattern to this solved the problem:
/you(?:\s[^\s]+)? see/
I need to check, if users input URL contains facebook(.any domain). This is what I have:
preg_match("/.(facebook)+\.[A-Z]{2,4}/", $input);
But on typying www.facebook.com it returns false. Could someone help me with this? I am not very good in regex.
That is because you are only matching for uppercase letters in the last part. You may want to make it match case independent by adding a modifier:
preg_match("/.(facebook)+\.[A-Z]{2,4}/i", $input);
The next things are:
you don't need to put "facebook" into a group
You don't need to quantify facebook
if you want to match a dot, then escape it
So end up with this:
preg_match("/\.facebook\.[A-Z]{2,4}/i", $input);
you can also try this along with the answer of #stema
if(strpos($url, "facebook") !== FALSE)
{
echo "exists";
}
else
{
echo "not exists";
}
How to match any thing dosen't contain a specific word using RegExp
Ex:
Match any string doesn't contain 'aabbcc'
bbbaaaassdd // Match this
aabbaabbccaass // Reject this
If you're just after this sequence of characters, don't use a Regular Expression. Use strpos().
if (strpos('aabbaabbccaass', 'aabbcc') !== false) {
echo 'Reject this.'
}
Note: Be sure to read the warning in the manual about strpos() return values.
You can use negative lookahead:
(?!.*?aabbcc)^.*$
Live Demo: http://www.rubular.com/r/4Exbf7UdDv
PHP Code:
$str = 'aabbaabbccaass'; //or whatever
if (preg_match('/(?!.*?aabbcc)^.*$/', $str))
echo "accepted\n";
else
echo "rejected\n";
try this to avoid some sequences of letters :
^((?!aabbcc).)*$
try this:
if(preg_match('/aabbcc/', $string) == 0) {
[ OK ]
}
else {
[ NOT OK ]
}
You can use this to describe a substring that doesn't contain aabbcc:
(?>[^a]++|a(?!abbcc))*
for the whole string, just add anchors (^ $)
i need to write a case which only except the a-zA-Z0-9 characters with underscore and white space(1 or more than 1) and ignore all rest of the characters.I wrote a code but its not working properly.
In those case should be wrong but its show OK
1) test msg#
2) test#msg
3) test!msg
also those should be OK but currently shows wrong.
1) test msg.-(Two white space)
what i should to change in my code .pls help and see my code below.
$message=$_GET['msg'];
if(preg_match('/[^A-Za-z0-9]\W/',$message))
{
echo "Wrong";
}
else
{
echo "OK";
}
Here's an optimized version of the one left by riad:
$message = $_GET['msg'];
if ( preg_match('/^[a-z0-9_ ]+$/i', $message) )
{
echo 'Ok';
}
else
{
echo 'Wrong';
}
I've removed the A-Z (uppercase) from the regular expression since the i modifier is used.
I'd also like to explain what you did wrong in the example you provided.
First, by putting the ^ inside the square brackets ([]), you're essentially doing the opposite of what you were trying to do. Place a ^ inside the square brackets means "not including."
You were missing a *, + or ? at the end of the square bracket, unless you only wanted to match a single character. The * character means 0 or more, + means 1 or more and ? means 0 or 1.
The \W means any non-word character. That's probably not what you wanted.
Finally, to starting a regular expression with ^ means that the beginning of the string you're string to match must start with whatever is after the ^. Ending the regular expression with a $ means that the string must end with the characters preceding the $.
So by typing /^[a-z0-9_ ]+$/i you're saying match a string that starts with a-z0-9_ or a space, that contains at least of those characters (+) and ends.
PHP has a lot of documentation of the PCRE regular syntax which you can find here: http://ca2.php.net/manual/en/reference.pcre.pattern.syntax.php.
$message=$_GET['msg'];
if(preg_match('/^[a-zA-Z0-9_ ]+$/i',$message))
{
echo "Wrong";
}
else
{
echo "OK";
}
I'm trying to find special characters with characters like <?, <?php, or ?> in a string. The code below works to find "php" in the string anywhere no matter if it's PHP, php, or phpaPHPa.
<?php
$searchfor = "php";
$string = "PHP is the web scripting language of choice.";
if (preg_match("/".$searchfor."/i", $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
I need a similar code that finds special characters like <?, <?php, or ?> in the string. Any suggestions?
You can use same code. Just make sure to escape regex special characters when using them in matching. The question mark must be escaped so your $searchfor becomes <\?php
Using strpos (or stripos) will be faster than using RegEx'es...
if(false === strpos($text, '<?'))
echo 'Match was not found';
else
echo 'Match was found';