DevSael Blog. C, CPP, C++, PHP, .NET Programming BLOG! advanced, MySQL, MongoDB, RDBMS.

Browsing "Older Posts"

Browsing Category "php regexp"

If you are looking for a php algorithm to rank search results based on proximity/relevance of multiple words here comes a quick and easy way of generating search results with PHP only:
Issues with the other boolean search methods such as strpos()preg_match()strstr() or stristr()
  1. can't search for multiple words
  2. results are unranked
It sounds difficult but is surprisingly easy.
If we want to search for multiple words in a string the core problem is how we assign a weight to each one of them?
If we could weight the terms in a string based on how representative they are of the string as a whole, we could order our res
ults by the ones that best match the query.

On the performance side, use the strpos function. It is about three times faster and have in mind, when I did one million compares at once, it took preg match 1.5 seconds to finish and for strpos it took 0.5 seconds. What I'm trying to say is that it runs really fast either way.
if (strpos($a,'are') !== false) {
    echo 'true';
}
Note that the use of !== false is deliberate; strpos returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').

Another option is to use the strstr() function. Something like:

if (strlen(strstr($haystack,$needle))>0) {

// Needle Found

}
Point to note: The strstr() function is case-sensitive. For a case-insensitive search, use the stristr()function.

Not Using REGEXP:


function contains($text, $word) { $found = false; $spaceArray = explode(' ', $text); $nonBreakingSpaceArray = explode(chr(160), $text); if (in_array($word, $spaceArray) || in_array($word, $nonBreakingSpaceArray) ) { $found = true; } return $found; }
Make use of case-insensitve matching using stripos():
if (stripos($string,$stringToSearch) !== false) {
    echo 'true';
}
strstr() function is case-sensitive. For a case-insensitive search, use the stristr()function.

String can be checked with the below function

function either_String_existor_not($str,$character){
    if (strpos($str,$character) !== false) {
        return true;
    } else{
        return false;
    }
}
---end---

Best function to check if string contains specific words?

By Game Changer → Saturday, December 5, 2015

Hi Dude,
Actually there is no simple regular expression for this problem.  Confirmation email tokens are the only correct way to know you got the address of the person entering it. This is why most mailing lists now use that mechanism to confirm sign-ups. After all, anybody can put down president@whitehouse.gov, and that will even parse as legal, but it isn't likely to be the person at the other end.
This regular expression will only validate addresses that have had any comments stripped and replaced with whitespace (this is done by the module).
If you're looking for something simpler but that will catch most valid email addresses try something like:
"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
Remember, After all, the best way to validate the email address is still to actually send an email to the address in question to validate the address. If the email address is part of user authentication (register/login/etc), then you can perfectly combine it with the user activation system. I.e. send an email with a link with an unique activation key to the specified email address and only allow login when the user has activated the newly created account using the link in the email.
If the purpose of the regex is just to quickly inform the user in the UI that the specified email address doesn't look like in the right format, best is still to check if it matches basically the following regex:
^([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)$
Not to mention that non-Latin (Chinese, Arabic, Greek, Hebrew, Cyrillic and so on) domain names are to be allowed in the near future. Everyone has to change the email regex used, because those characters are surely not to be covered by [a-z]/i nor \w. They will all fail.

What's the best regular expression you have or have seen for validating emails?

By Game Changer →