For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > November 2005 > Programming a general search in PHP









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Programming a general search in PHP
marcoponte

2005-11-17, 6:56 pm

I have a string S and i want to verify if specific substrings S1, S2,
.... occur in S according to any possible boolean expression. E.g.
return a value of TRUE if (S1 AND S2) OR (S3 AND S4) AND NOT S5 occur
in S. S is considered to be a continuous stream of characters

How do i program this in PHP, where the boolean expressions can be
anything ?

Stefan Rybacki

2005-11-17, 6:56 pm

marcoponte wrote:
> I have a string S and i want to verify if specific substrings S1, S2,
> ... occur in S according to any possible boolean expression. E.g.
> return a value of TRUE if (S1 AND S2) OR (S3 AND S4) AND NOT S5 occur
> in S. S is considered to be a continuous stream of characters


In what style do you get the boolean expression? A string?

Regards
Stefan

>
> How do i program this in PHP, where the boolean expressions can be
> anything ?
>

Berimor

2005-11-17, 6:56 pm

On 17 Nov 2005 07:23:17 -0800, marcoponte <marco_ponte@yahoo.com> wrote:

> I have a string S and i want to verify if specific substrings S1, S2,
> ... occur in S according to any possible boolean expression. E.g.
> return a value of TRUE if (S1 AND S2) OR (S3 AND S4) AND NOT S5 occur
> in S. S is considered to be a continuous stream of characters
>
> How do i program this in PHP, where the boolean expressions can be
> anything ?
>


you have alredy wrote it by yourself :)
if (S1 AND S2) OR (S3 AND S4) AND NOT S5

if (((strpos($S,$S1) && strpos($S,$S2)) ||
((strpos($S,$S3) && strpos($S,$S4)) && !strpos($S,$5)){
// rest of code
}



--
Exact Meta Search | Major Search Engine
http://exactsearcher.comif (S1 AND S2) OR (S3 AND S4) AND NOT S5
Hilarion

2005-11-18, 7:55 am

>> I have a string S and i want to verify if specific substrings S1, S2,
>
> you have alredy wrote it by yourself :)
> if (S1 AND S2) OR (S3 AND S4) AND NOT S5
>
> if (((strpos($S,$S1) && strpos($S,$S2)) ||
> ((strpos($S,$S3) && strpos($S,$S4)) && !strpos($S,$5)){
> // rest of code
> }



It's not that easy. "strpos" returns zero when the text was found
at position zero and returns false when the text was not found.
Both of those results when treated as boolean are evaluated
to false. Which means that your solution will not work correctly.
Test it for case when $S does contain $S3 and $S4 and does
not contain $S1, $S2, $S5, and $S3 or $S4 is at the very start
of $S, eg.:

$S = '34';
$S1 = '1';
$S2 = '2';
$S3 = '3';
$S4 = '4';
$S5 = '5';

The result of evaluation should be true, but your solution gives
false. You'd have to replace all "strpos( $S, $SX )" with
"(strpos( $S, $SX ) !== FALSE)" and all "!strpos( $S, $SX )"
with "(strpos( $S, $SX ) === FALSE)".

Another thing is: should "(S1 AND S2) OR (S3 AND S4) AND NOT S5" be
evaluated as "((S1 AND S2) OR (S3 AND S4)) AND NOT S5" or rather
as "(S1 AND S2) OR ((S3 AND S4) AND NOT S5)". This is a question for
Marcoponte, not to Berimor.As far as I remember PHP will choose the
second one (which is quite common for most boolean expression
evaluation methods because usually "AND" operator has higher
precedence/priority than "OR" operator).


Hilarion
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com