Jump to content

Recommended Posts

Folks,

 

I want to find a Word in a string. If the word is found, i want to retrun TRUE else FALSE.

 

$string: Sol 3 Drawer Chests Antique Pine Bedside Chest

 

$word to be found: Chest

 

Now the tricky part is: In the $string the $word occurs twice once its CHESTS (i mean the first occurance) then CHEST.

 

If its found, i want to return TRUE else FALSE.

 

I tried using strpos() but it just checks for CHEST and not CHESTS.

 

How can it be achieved?

 

Cheers

Natasha

Link to comment
https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/
Share on other sites

The strpos function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

 

$string = "Sol 3 Drawer Chests Antique Pine Bedside Chest";
$word = "Chest";

$pos = strpos(strtolower($string), strtolower($word));

// Note our use of ===.  Simply == would not work as expected
// because the position of 'chest' MIGHT be the 0th (first) character.
if ($pos === false) 
{
//the string was not found
}
else
{
//the string WAS found
}

in ur case, stristr() is so very unreliable. Used preg_match() for an absolute error free. It is multifold reliable than the former.

 

Helllo,

 

In what sense is it not reliable, i sit no case Insensitive?

 

If so, could you tell me what preg_match() query i need to use to achieve my requirement?

 

Cheers

Natasha

preg_match() function in php i a regular search expression. For details of its usage, please visit http://www.php.net

 

Here is a sample of code that i made which i believe will be useful to you :)

 

FOR CASE SENSITIVE SEARCH

<?php
$string="Sol 3 Drawer Chests Antique Pine Bedside Chest";
$word='Chest';
if(preg_match("*$word*",$string))
{
echo $word." "."found";
}
else
echo $word." ".'not forund';
?>

 

FOR CASE-INSENSITIVE SEARCH

<?php
$string="Sol 3 Drawer Chests Antique Pine Bedside Chest";
$word='chest';
if(strcasecmp($word,$string))
{
echo $word." "."found";
}
else
echo $word." ".'not forund';
?>

 

And why not use preg_match with boundary which is the \b and the i for insensitive search?

 

untested, but it should work

<?php
function matchWord($word,$string){
if (preg_match("/\$word\b/i", $string)) {
$the_result = "match";
} else {
$the_result = "no match";
}
return $the_result;
}

//usage
$text="Sol 3 Drawer Chests Antique Pine Bedside Chest";
$the_word = "Chest";
$is_it_there = matchWord($the_word,$text);
echo $is_it_there;
?>

well i didn't escape the last variable, but I'm guessing don't want the boundaries so left it out

 

<?php
function matchWord($word,$string){
if (preg_match("/$word/i", $string)) {
$the_result = "match";
} else {
$the_result = "no match";
}
return $the_result;
}

//usage
$text="Sol 3 Drawer Chests Antique Pine Bedside Chest";
$the_word = "Chest";
$is_it_there = matchWord($the_word,$text);
echo $is_it_there;
?>

Thanks everyone.

 

Thanks Quicky.

 

Another thing, i used this nice little monster to singularize the plural along with the codes you guys supplied, for enhancement.

 

<?php

function depluralize($word){

    // Here is the list of rules. To add a scenario,

    // Add the plural ending as the key and the singular

    // ending as the value for that key. This could be

    // turned into a preg_replace and probably will be

    // eventually, but for now, this is what it is.

    //

    // Note: The first rule has a value of false since

    // we don't want to mess with words that end with

    // double 's'. We normally wouldn't have to create

    // rules for words we don't want to mess with, but

    // the last rule (s) would catch double (ss) words

    // if we didn't stop before it got to that rule.

    $rules = array(

        'ss' => false,

        'os' => 'o',

        'ies' => 'y',

        'xes' => 'x',

        'oes' => 'o',

        'ies' => 'y',

        'ves' => 'f',

        's' => '');

    // Loop through all the rules and do the replacement.

    foreach(array_keys($rules) as $key){

        // If the end of the word doesn't match the key,

        // it's not a candidate for replacement. Move on

        // to the next plural ending.

        if(substr($word, (strlen($key) * -1)) != $key)

            continue;

        // If the value of the key is false, stop looping

        // and return the original version of the word.

        if($key === false)

            return $word;

        // We've made it this far, so we can do the

        // replacement.

        return substr($word, 0, strlen($word) - strlen($key)) . $rules[$key];

    }

    return $word;

 

}

?>

  • 1 year later...

Hi

 

Just a note to say that there is a minor bug in the above function. It checks for a key being false in the array of rules (to cope with a string ending ss), yet it needs to check for the value instead.

 

Minor change to correct this

 

<?php
function depluralize($word){
    // Here is the list of rules. To add a scenario,
    // Add the plural ending as the key and the singular
    // ending as the value for that key. This could be
    // turned into a preg_replace and probably will be
    // eventually, but for now, this is what it is.
    //
    // Note: The first rule has a value of false since
    // we don't want to mess with words that end with
    // double 's'. We normally wouldn't have to create
    // rules for words we don't want to mess with, but
    // the last rule (s) would catch double (ss) words
    // if we didn't stop before it got to that rule. 
    $rules = array( 
        'ss' => false, 
        'os' => 'o', 
        'ies' => 'y', 
        'xes' => 'x', 
        'oes' => 'o', 
        'ies' => 'y', 
        'ves' => 'f', 
        's' => '');
    // Loop through all the rules and do the replacement. 
    foreach($rules as $key=>$value){
        // If the end of the word doesn't match the key,
        // it's not a candidate for replacement. Move on
        // to the next plural ending. 
        if(substr($word, (strlen($key) * -1)) != $key) 
            continue;
        // If the value of the key is false, stop looping
        // and return the original version of the word. 
        if($value === false) 
            return $word;
        // We've made it this far, so we can do the
        // replacement. 
        return substr($word, 0, strlen($word) - strlen($key)) . $rules[$key]; 
    }
    return $word;

} 
?>

 

Useful function but this minor issue caught me out.

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.