I have the following function to do a "exact match" of a pattern($searchPat)
in a sentence ($sourceStr)
function isUsed($sourceStr, $searchPat) {
if (strpos($sourceStr, $searchPat) !== false) {
return true;
} else {
return false;
}
}
However, this doesn't do an exact match. I changed the function as follows but this doesn't even execute.
function isUsed($sourceStr, $searchPat) {
if (preg_match("~\b[$sourceStr]\b~", $searchPat)) {
return true;
} else {
return false;
}
}
How could I do an exact match please?
The [] is a character class. That lists characters you want to allow, for example [aeiou] would allow a vowel. Your variables are also in the inverted order, pattern first, then string to match against. Try this:
function isUsed($sourceStr, $searchPat) {
if (preg_match("~\b$searchPat\b~", $sourceStr)) {
return true;
} else {
return false;
}
}
Additional notes, this is case sensitive, so Be won't match be. If the values you are passing in are going to have special characters the preg_quote function should be used, preg_quote($variable, '~'). You also may want to concatenate the variable so it is clear that that is a variable and not part of the regex. The $ in regex means the end of the string.
Try This.
function isUsed($sourceStr, $searchPat) {
if (preg_match("/\b".preg_quote($sourceStr)."\b/i", $searchPat)) {
return true;
} else {
return false;
}
}
Please try "preg_match" for matches.
$string = 'test';
if ( preg_match("~\btest\b~",$string) )
echo "matched";
else
echo "no match";
Or try like this
if(stripos($text,$word) !== false)
echo "no match";
else
echo "match";
You can try this one:
function isUsed($string_to_search, $source_String) {
if (preg_match("/$string_to_search/", $source_String)) {
return true;
} else {
return false;
}
}
You can change according to your need.
Case insensitive: preg_match("/$string_to_search/i", $source_String)
Boundry condition: preg_match("/\b$string_to_search\b/i", $source_String)
Special characters: if you have any special characters in your string for your safe side replace it with '\special_character'
Related
I am using 2 regex functions here and I wanna make another function which returns false when the 2 regex are both false and if not, then true.
The problem here is when I wanna use the 2 regex functions in the third one, I have to give them parameters, which is not necessary I think, because the third function will only return a simple true or false. I get an undefined variable whenever I give parameters to the 2 regex functions in the 3rd one.
I tried using global variables which works but since its a bad practice I am looking for a better solution.
Code:
function regex1($input)
{
$regex= "/^[A-Za-z0-9 ]*$/";
if (!preg_match($regex, $input))
{
return false;
}
else
{
return true;
}
}
function regex2($input)
{
$regex= "/^[A-Za-z0-9 ]*$/";
if (!preg_match($regex, $input))
{
return false;
}
else
{
return true;
}
}
function checkBoth()
{
if (regex1($input) === false || regex2($input) === false)
{
return false;
}
else
{
return true;
}
}
EDIT:
The checkBoth function I am using in my other file like this together with the other 2 regex functions:
if (!regex1($input))
{
// show error at the same time
}
if (!regex2($input))
{
// show error at the same time
}
if(checkBoth())
{
// success
}
function regex2($input,$secondVar=false)
{....
Later in code in place where you need just add:
if($secondVar !== false){
// do whatever...
}
If you can't user "false" you can just empty string '' or any other value that will not appear there.
My pattern matching expression is like this i want to check and return error message.
public function checkthe($str)
{
if(preg_match('/^\(?[+]?[0-9]{3,4}[-. ]?[0-9]{8,10}$/', $str)==TRUE)
{
return true;
}
else
{
$this->form_validation->set_message('checkthe', 'The %s should be in format XXX-XXXXXXXX');
return false;
}
}
I need display error if other than + / and 0-9 in the string
If that is indeed the case your regex code should be:
public function checkthe($str) {
return preg_match('~^[+/0-9]+$~', $str);
}
Though it is always better to include your sample inputs.
Can any one help me to write a preg_match rules to detect whether an input string is a unicode code character?
Here is the list of characters:
http://www.utf8-chartable.de/unicode-utf8-table.pl?start=9728&number=128&utf8=string-literal
I want to write a methods to detect whether the input string is a emoticons
function detectEmoticons($input) {
if (preg_match("/REGEX/", $input)) {
return TRUE;
} else {
return FALSE;
}
}
If the input is a string like "\xe2\x98\x80" or "\xe2\x98\x81"... etc (all the chacracter available in the list http://www.utf8-chartable.de/unicode-utf8-table.pl?start=9728&number=128&utf8=string-literal) then it should return
TRUE
Thanks in Advance,
Uttam
First, use the u modifier if you want your regular expression to work with unicode. Second, use a character class for all characters in the range [\x{2600}-\x{267F}] (i.e. U+2600 to U+267F). Now you can write your function as:
function detectEmoticons($input){
if(preg_match("/[\x{2600}-\x{267F}]/u", $input)){
return TRUE;
}
else{
return FALSE;
}
}
For match Unicode characters in a Regular expressions you must add the u Modifier
Example:
function detectEmoticons($input) {
if (preg_match("/REGEX/u", $input)) {
return TRUE;
} else {
return FALSE;
}
}
If you must retrieve one of the set you could pass the character range like
/[\x{START}-\x{END}]/u
Or check all characters with the mb_strpos function
Example
function detectEmoticons($input) {
$characters = array("\xe2", "\x98", ...);
foreach ($characters as $v) {
if (mb_strpos($input, $v) !== false)
return true;
}
return false;
}
You can find the documentation here:
http://ch1.php.net/manual/en/reference.pcre.pattern.modifiers.php
http://ch2.php.net/manual/en/function.mb-strpos.php
try this
preg_match("/\\[a-zA-Z0-9_-]{1,}\\[a-zA-Z0-9_-]{1,}\\[a-zA-Z0-9_-]{3}/", $input);
Use preg_replace to scape
preg_replace("/\\[a-zA-Z0-9_-]{1,}\\[a-zA-Z0-9_-]{1,}\\[a-zA-Z0-9_-]{3}/",'', $input);
I loop through the values of a form, to check that each field has 4 digits. My problem is currently it validates true or false only on the match for the first field $card1...
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++)
if (! preg_match ($regex,$cards[$i]))
{
return false;
}
else
{
return true;
}
}
You're returning (by using return ...) something in the first iteration every time (boolean condition with an else).
You need to put the return true outside the loop statement:
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++) {
if (! preg_match ($regex,$cards[$i])) {
return false;
}
}
return true;
}
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++)
if (! preg_match ($regex,$cards[$i]))
{
return false;
}
return true;
}
I have this block of code
public function onlyLetters($string) {
if(preg_match("/[a-zA-Z]/", $string)) {
return true;
} else {
return false;
}
}
but it always returns false, what would the reason be?
Your string most likely doesn't contain what you expect, and doesn't have a single upper or lower case a-z letter.
OR - you aren't handling the return value correctly
If you check these assumptions, you'll probably solve it yourself :)