Query value within a variable

Asked

Viewed 3,371 times

3

I wonder if you have how to use php to do the following let’s say I have a value within a variable

$text = "here is the member text value"

Inside the value of this variable I would like to consult only the specific word in the case in question the member word if it exists the result is true if it does not exist the result is false you can do it using php ?

Update 02/06/2015: Thanks in advance for the help only I’m now with another problem I used both answers to do the following I put these urls https://cloud.mail.ru and https://my.pcloud.com of them I did a word search pcloud in the first and cloud in the second the problem and that this presenting conflict is if it has the url https://my.pcloud.com.

There if I’m looking cloud of the true result being that it was to give false in the text pcloud has some way of complementing both these codes of the answers to make the word query exactly or checking the beginning of the word consulted at the end of it to avoid this kind of error ?

Literally the error is not manipulated if the last letter of the word is different and not the first letter.

  • 1

    first reply to my dear page:
 
 http://stackoverflow.com/questions/1019169/how-can-i-check-if-a-word-is-contained-in-another-string-using-php

3 answers

4


Yes it is possible this using regular expressions, use the function preg_match whether the pattern of regex exists in the string passed as second argument.

function findKeyWord($str, $key){
    $regex = '/'. $key .'/i';
    return preg_match($regex, $str);
}

echo findKeyWord("aqui fica o valor do texto do membro", 'membro');
echo findKeyWord("aqui fica o valor do texto do membro", 'membros');

The exit to the first call is 1 for true and the second 0 for false.

Example - Ideone

Updating

To find an exact value use the anchor \b - edge, when using it you denote the limits of the word you want marry, where it starts and ends, see an example:

function encontrarValor($texto, $palavra){
    if (preg_match("%\b{$palavra}\b%", $texto))
      return true;
    else
      return false;
}

Example of use:

$texto1 = "aqui fica o valor do texto do ZZZmembroYYY";
$texto2 = "aqui fica o valor do texto do membro";

$resultado1 = encontrarValor($texto1, "membro");
$resultado2 = encontrarValor($texto2, "membro");

if ($resultado1)
  echo "(membro) foi encontrado em {$texto1} \n";
else 
  echo "Não foi possível encontrar (membro) em ({$texto1}) \n";

if ($resultado2)
  echo "(membro) foi encontrado em ({$texto2}) \n";
else
  echo "Não foi possível encontrar (membro) na ({$texto2}) \n";

Example - Ideone

  • In the type case let’s say you want to use this in a Function and want to consult the word member if I use this code and have the word members in the plural he will know to define that I only want members in the singular or if I will perform the query in this case if the query is member singular and is in the text in question as members the result will give false ?

  • In case he gives 1 to true and 0 to false ? Ai if I want to apply this with use of if I will have to put if it is = 1 true and < 1 false ne pulling of the function findKeyWord putting it inside a right if ?

  • No, php interprets some values as false, as zero, empty string, null. No need to compare if it is greater than 1. @Rodrigo

  • I thank you both for the answers both for the answers both will be very helpful to me.

  • @rray I went wild and edited your answer, to answer what was asked in the question update, anything you can reverse or improve it. =)

  • @Rodrigo See the response update.

  • Okay thanks for the help I don’t think you were nosy because you just wanted to help.

  • 1

    @qmechanik, q cool edition :D, thanks. I thought you were going to answer this question with regex, I’ve seen other answers q vc uses regex, are pretty cool.

Show 3 more comments

3

You can use the function strpos.

$texto = 'aqui fica o valor do texto do membro';
$procurar = 'membro';
$pos = strpos($texto , $procurar);

if ($pos === false) {
    echo "A string {$procurar} não foi encontrada na string {$texto}";
} else {
    echo "A string {$procurar} foi encontrada na string {$texto}";
    echo " e existe na posição {$pos}";
}

See demonstração

To do this in a way that doesn’t differentiate between upper and lower case, use stripos().

To return true or false, if a value exists in a text, do:

function consultarValor($texto, $procurar){
    $pos = strpos($texto , $procurar);
    if ($pos === false)
      return false;
    else
      return true;
}

$texto = 'aqui fica o valor do texto do membro';
$procurar = 'membro';

if (consultarValor($texto, $procurar)){
    // Fazer algo aqui caso $valor existe em $texto.
} else {
    // $procurar não existe em $texto.
}

See demonstração

2

Check function mb_stripos()

Function Mb_* are functions of the library MBSTRING, are functions that provide multibyte character support.

The letter "i" in the function name, indicates "marry insensitive"

The function returns the position of the first occurrence of the string within the text and, if not found, returns boolean false

Browser other questions tagged

You are not signed in. Login or sign up in order to post.