Jump to content

[SOLVED] Preg_Match not working


liamthebof

Recommended Posts

OK, the tital is a lie, I just cant get it to work.

SO far:

 

$animal = $_POST["animal"];

^From a form input box, works perfect.

 

if ($animallist = fopen("currentanimals.cfg", 'r')) {

 

$list = stream_get_contents($animallist, -1, 0);

 

if(preg_match("|$animal|",$list) === TRUE)

{

echo "Already present";

}

else {

echo "Will be added";

}

 

members.cfg

Cat

Dog

Fish

Money

 

OK, I would like it so when someone enters Dog in my form, this bit of php searches my animal list and checks if what it searched for was there. If so, say 'Already present', else say, 'To be added'. However, when I enter Dog, it says 'To be added'.

 

Why?

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/117896-solved-preg_match-not-working/
Share on other sites

There are much simpler and more efficient ways to do it...

 

<?php
$list = file_get_contents('currentanimals.cfg'); // Easier on the eyes than your current implementation 
if(stripos($list, $animal) === false) // stripos is faster than preg_match, and is case insensitive... if you want case sensitivity use strpos()
{
   echo 'Will be added';
}
else {
   echo 'Already present';
}

 

sorry I kind of dodged your question

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.