Jump to content

Recommended Posts

Hi,

 

Please see the code below:

 

$string = 'I want to be a PHP geek!';

 

if (preg_match('/'. urldecode($_GET['no']) . '/i', $string))

 

//do something

 

issue 1:

When I search with parenthesis '(' I got the following error:

 

Warning: preg_match(): Compilation failed: missing ) at offset 1

 

and with ')':

 

Warning: preg_match(): Compilation failed: unmatched parentheses at offset 0

 

 

isseu 2:

I want to search letter by letter; please consider the following snippet:

 

$string = 'Do you want to be a PHP geek?';

 

preg_match('/php want/i', $string) ? $var = 'Cool': $var = 'Noooooo';

 

 

I want it to have me the $var "Cool". How can I get it done please?

 

thanks for helping me.

Link to comment
https://forums.phpfreaks.com/topic/270820-using-preg_match/
Share on other sites

Do you check if $_GET is set before you use it?

Though, I only get Undefined index: no, when I don't set it.

 

<?php
$string = 'some text test';
if (preg_match('/'. urldecode($_GET['no']) . '/i', $string)) echo 'TRUE: '.$_GET['no'];
else echo 'FALSE '.$_GET['no']
?>

^ Worked as expected for me.

 

results:

TRUE: test

TRUE: text

FALSE string

TRUE: some

FALSE some text string

TRUE: some text test

etc

 

BUT watch out for special characters like (, ) and many others:

Warning: preg_match() [function.preg-match]: Compilation failed: unmatched parentheses at offset 4

FALSE some)

Warning: preg_match() [function.preg-match]: No ending delimiter '/'

FALSE some\

Warning: preg_match() [function.preg-match]: Unknown modifier '/'

FALSE some/

 

I would strongly suggest not using user input in the regex pattern, unless you are very sure you've tested it to be safe already.

Edited by MMDE
Link to comment
https://forums.phpfreaks.com/topic/270820-using-preg_match/#findComment-1393137
Share on other sites

Thanks for the reply.

 

I mean I need a multiple strings searched with no sequential or single string.

 

if the string given is as below:

 

“In veterinary school we studied the brain of the hippopotamus. At that time most students stayed on the main campus, while I stayed on the hippocampus.”

 

and I enter "campus school(bolded)" at the same time I want it to be true. I think preg_match searches one sting or adjacent strings only.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/270820-using-preg_match/#findComment-1393139
Share on other sites

So, if I understand you correct, the resulting RegExp you're trying to match to the string is the following?

'/campus school(bolded)/i'

 

If so, then you've clearly misunderstood what Regular Expressions are, and how to actually use them. The above RegExp will only match a string containing the text "campus schoolbolded", and save the "bolded" part in a sub group of its own.

While Regular Expressions are a tool to parse text written in any regular language, it is not a natural language reader. It is simply a tool which can be used to define the rules of the patterns you want to match, the context and grammatical implications is something you have to take into account yourself. All the RegExp engine can do, is try to match the text against the pattern you've specified.they do what they do.

 

Should I have misunderstood your intent, please explain a bit further on what it is, exactly, you're trying to do.

Link to comment
https://forums.phpfreaks.com/topic/270820-using-preg_match/#findComment-1393142
Share on other sites

To further extend upon what Christian F. said...

You can easily write a script that makes a regex which will make preg_match return true if at least one of the words in the input are in the string.

word1|word2|word3

etc

 

You can also make preg_match return true if one of the words input are found as a whole word in the script, and not the part of a word:

((\W|^)word1(\W|$))|((\W|^)word2(\W|$))|((\W|^)word3(\W|$))

 

The hard part comes when you want it to contain all the inputted words in one regex pattern... Then you get a very long permutated string, which I totally don't recommend (might be there is some way around it I don't know about). In this case I think a better option would be to loop through all the words and make sure you find them all in the string.

 

function match_input($input, $string){

$one_match = false;

$words = explode(' ', $input);

$wc = count($words);

for($i=0; $i<$wc; i++){

if(preg_match('/\A\s\Z/', $words[$i])){

continue;

}

if(!preg_match('/(\W|^)'.$words[$i].'(\W|$)/', $string)){

return false;

}else{

$one_match = false;

}

}

if($one_match){

return true;

}

return false;

}

 

Sorry, did a couple of typos, but I think I've fixed 'em all now!

I also just added another small change to one of my codes, to check if the $words[$i] only contained white-space characters, and to ignore those, and also to return false if no words was actually found in the string.

Edited by MMDE
Link to comment
https://forums.phpfreaks.com/topic/270820-using-preg_match/#findComment-1393145
Share on other sites

If you need to use user input in a regular expression, you should run it through preg_quote first. That will escape any special characters so the user expression is searched as a literal string.

 

$string = 'I want to be a PHP geek!';

if (preg_match('/'. preg_quote($_GET['no'], '/') . '/i', $string))

//do something

 

This should get rid of the compilation error when the user's input contains unbalanced parenthesis

Link to comment
https://forums.phpfreaks.com/topic/270820-using-preg_match/#findComment-1393147
Share on other sites

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.