Jump to content

Put a preg_match function into an array to call later.


Recommended Posts

Ok guys.  Here is what I got.  I have one preg_match function that depending on the circumstances can be called two different ways.

 

The first way the function is like this

                          if (preg_match("/$liveone/", $content) || preg_match("/$livetwo/", $content) ||
                            preg_match("/$livethree/", $content) || preg_match("/$livefour/", $content) ||
                            preg_match("/$livefive/", $content))

 

Where it only has to match one of the items with contents on the page.

 

Then the other is like this

 

                          if (preg_match("/$liveone/", $content) | preg_match("/$livetwo/", $content) |
                            preg_match("/$livethree/", $content) | preg_match("/$livefour/", $content) |
                            preg_match("/$livefive/", $content))

 

Where it must match all the itmes with the contents on the page.

 

The way it is called depends on a setting in a setting table in my database.  I want to put this into an array or something where I can call it like this.

 

if($requireall == Y)
{
$requireall;
}
else
{
if($requireall == N)
{
$requireone;
}
else
{
echo "Error";
}
}

 

I thought about just putting each into an include file and calling that file to include that one but it didn't work that way for some reason.

<?php
function myMatch($content, $which='one') {
     if ($which == 'one') {
             if (preg_match("/$liveone/", $content) || preg_match("/$livetwo/", $content) ||
                    preg_match("/$livethree/", $content) || preg_match("/$livefour/", $content) ||
                    preg_match("/$livefive/", $content)) {
                    return true;
              }
     }elseif ($which == 'all') {
              if (preg_match("/$liveone/", $content) | preg_match("/$livetwo/", $content) |
                            preg_match("/$livethree/", $content) | preg_match("/$livefour/", $content) |
                            preg_match("/$livefive/", $content)) {
                    return true;
               }
     }

      return false;
}

if($requireall == Y)
{
       myMatch($content, 'all');
}
else
{
if($requireall == N)
{
       myMatch($content, 'one');
}
else
{
echo "Error";
}
}
?>

 

Like that?

Something like that but I am getting an error.  Here is the code I have now.

 

<?
include 'header.php';
echo "\r\n";
$nowtime = date('F d Y');
$currenttime = strtotime("$nowtime");
include 'functinos.php';
?>
<div class="borderwrap">
<div id="logostrip"><a href="<?PHP echo "$linkboturl"; ?>/"><img src="<?PHP echo
"$imagedir"; ?>/linkbot.png" alt="LinkBOT" style="vertical-align: top;" align="left" border="0"></a></div></div>
<table id="row2" width="100%" cellpadding="0" cellspacing="1" style="text-align:center;font-family:'Trebuchet MS',sans-serif;font-size:1.3em;">
<tr>
  <td class="row2" align="left">Welcome: <? echo "$admitname"; ?>
  </td>
  <td class="row2">LinkBOT Status:
  <?PHP
if ($lbstatus == 0)
{
    echo "<font color=\"#990033\"> Offline </font>";
}
else
{
    if ($lbstatus == 1)
    {
        echo "<font color=\"#00CC00\"><nobr> Online </nobr></font>";
    }
    else
    {
        echo "<font color=\"#firebrick\">Error</font>";
    }
}
?>
  </td>
  <td class="row2">
  LinkBOT Help
  </td>
</tr>
</table>
<table id="row2" width="100%" cellpadding="0" cellspacing="1" style="text-align:center;font-family:'Trebuchet MS',sans-serif;font-size:1.3em;">
<tr>
  <td class="row2" align="left">Full Auto
  </td>
  <td class="row2">Semi Auto
  </td>
  <td class="row2"><font color=#009900>Manual</font></a>
  </td>
</tr>
</table>
<form method=post action=test.php><input type=hidden name=startnow value=go>
<center><textarea name=linkinput cols=110 rows=5 style="background: #ffffff; border: 1px solid #000000;"></textarea><br>
<center><input type="checkbox" name="hidelive" value="Y" checked="checked" />Hide Live Links &nbsp &nbsp &nbsp <input type=submit value="Verify Links" style="background: #ffffff; font-weight: bold; color: #000000;"></center></form>
<?php
if (isset($_POST["startnow"]))
{
?>
<div id="row3">
	<div style="float: left;"><img src="<?PHP echo "$imagedir/nav_m.gif"; ?>" alt=">" border="0" height="8" width="8"><font color="#FFFFFF"> LinkBOT Version 2.5</font></div>
	<div align="right"><font color="#FFFFFF">Manual Mode</font></div>
</div>
<table class="table" cellspacing="1">
	<tbody><tr>
		<th width="50"><center>Status</center></th>
		<th><center>Link</center></th>
	</tr>
<?
    $linkinput = $_POST["linkinput"];
    $hidelive = $_POST["hidelive"];
    $linkinput = str_replace("\r\n", " ", $linkinput);
    $linkinput = str_replace("http", ". http", $linkinput);
    $linkinput = str_replace(")", " )", $linkinput);
    $Exploded = explode(" ", $linkinput);
    $Ttl_Parts = count($Exploded);
    for ($i = 0; $i <= $Ttl_Parts; $i++)
    {
        if (((@strpos($Exploded[$i], "http")) === false) || ($Exploded[$i] == "") || ($Exploded[$i] ==
            " "))
        {
            unset($Exploded[$i]);
        }
    }
    $Newlinks = array_values($Exploded);
    $Max_Parts = count($Newlinks);
    for ($i = 0; $i < $Max_Parts; $i++)
    {
        $filepath = $Newlinks[$i];
        if (@fclose(@fopen("$filepath", "r")))
        {
            if ($handle = @fopen($filepath, "r"))
            {
                $content = "";
                $part = fread($handle, 50);
                $content .= $part;
                if (eregi("</body>", $part))
                    break;
                fclose($handle);
            }
            if (preg_match("/<!DOCTYPE/i", $content) || preg_match("/<html>/i", $content))
            {
                $parsedhost = parse_url($filepath);
                $host = $parsedhost[host];

                mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
                mysql_select_db("$dbase");
                $query = "SELECT * FROM servers WHERE (server='$host')";
                $result = mysql_query($query) or ("" . mysql_error());
                while ($row = mysql_fetch_array($result))
                {
                    extract($row);
                }
                if (($ifExists = mysql_num_rows($result)) && $ifExists >= 0)
                {
                    if ($handle = @fopen($filepath, "r"))
                    {
                        $content = "";
                        while (!feof($handle))
                        {
                            $part = fread($handle, 1024);
                            $content .= $part;
                            if (eregi("</body>", $part))
                                break;
                        }

if($requireall == Y)
{
       myMatch($content, 'all');
}
else
{
if($requireall == N)
{
       myMatch($content, 'one');
}
else
{
echo "Error";
}
}
                      {
                            if ($hidelive == Y)
                            {
                            }
                            else
                            {
                                echo "<tr><td id='row2' align='center'>";
                                if ($currenttime < $trialend)
                                {
                                    echo "<img src='$imagedir/check.gif'><img src='$imagedir/live.gif'>";
                                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                    flush();
                                }
                                else
                                {
                                    echo "<img src='$imagedir/live.gif'></td>";
                                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                    flush();
                                }
                            }
                        }
                        else
                        {
                            if (preg_match("/$deadone/", $content) || preg_match("/$deadtwo/", $content) ||
                                preg_match("/$deadthree/", $content) || preg_match("/$deadfour/", $content) ||
                                preg_match("/$deadfive/", $content))
                            {
                                if ($currenttime < $trialend)
                                {
                                    echo "<tr><td id='row2'><img src='$imagedir/check.gif'><img src='$imagedir/dead.gif'>";
                                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                    flush();
                                }
                                else
                                {
                                    echo "<tr><td id='row2' ><img src='$imagedir/dead.gif'></td>";
                                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                    flush();
                                }
                            }
                            else
                            {
                                echo "<tr><td id='row2'>Status: Error</td>";
                                echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                flush();
                            }
                        }
                    }
                }
                else
                {
                    echo "<tr><td id='row2'>Status: This Server is not supported</td>";
                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                    flush();
                }
            }
            else
            {
                echo "<tr><td id='row2'>Status:<font color=green> This is a direct file link.  The file exists.</font></td>";
                echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                flush();
            }
        }
        else
        {
            echo "<tr><td id='row2' >Status:<font color=green> This is a direct file link.  The file does not exist.</font></td>";
            echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
            flush();
        }

    }
}
echo "</table>";
include 'footer.php';
?>

What I want to do is use a database field to select wether the && or & is used.  If the database field for that setting is Y  I want to have to match all 5 of the other fields.  If it is set to N then I want it to only have to match 1 of those 5 fields.

 

I also tried this

 

$whichlive = array('|','||');
if($requireallive == Y)
{
$live = $whichlive[0];
}
else
{
$live = $whichlive[1];	
}         
                        if (preg_match("/$liveone/", $content) $live preg_match("/$livetwo/", $content) $live
                            preg_match("/$livethree/", $content) $live preg_match("/$livefour/", $content) $live
                            preg_match("/$livefive/", $content))

 

I don't mind if it uses | or & or andand.  As long as it works.

 

The error is this

 

Parse error: syntax error, unexpected T_VARIABLE in C:\Program Files\KeyFocus\KFWS\htdocs\test.php on line 130

 

The if(preg_match line is the line it says is erroring out.

Oops sorry.  forgot to put about 10 lines around the code for the error.

 

                          $part = fread($handle, 1024);
                            $content .= $part;
                            if (eregi("</body>", $part))
                                break;
                        }
$whichlive = array('|','||');
if($requireallive == Y)
{
$live = $whichlive[0];
}
else
{
$live = $whichlive[1];	

}         
                        if (preg_match("/$liveone/", $content) $live preg_match("/$livetwo/", $content) $live
                            preg_match("/$livethree/", $content) $live preg_match("/$livefour/", $content) $live
                            preg_match("/$livefive/", $content))

				   {
                            if ($hidelive == Y)
                            {
                            }
                            else
                            {
                                echo "<tr><td id='row2' align='center'>";
                                if ($currenttime < $trialend)
                                {
                                    echo "<img src='$imagedir/check.gif'><img src='$imagedir/live.gif'>";
                                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                    flush();
                                }
                                else

 

This is several lines before and after the preg_match line with the error.

No, I mean "|" and "&" are bitwise operators.  They're used in bit masks and the like.  && and || are the boolean AND and OR operators, respectively.  It doesn't look like you're doing any bit operations there; you want the boolean ones.

 

You're getting an error in that code because using a variable to choose an operator won't work (outside of an exec()).  The Zend engine is running into that variable, $live, and saying, "WTF is this T_VARIABLE doing here?" because it's not expected.

 

Another thing, will those $live{one,two,...,five} variables ever contain "special" regex characters that are to be interpreted as literals?  If so, you should be using preg_quote.

Okay, I thought you might be using constant strings instead of regex patterns.  You'll want to use strpos or stripos (ignore case, A vs. a) instead of preg_match for constant strings; it'll increase performance.

 

Try something like this:

 

<?php

function find_stuff ($needles,$haystack,$behavior = 'all') {
// $behavior should either be 'all' or 'any'
// $needles is an array
// $haystack is a string

foreach ($needles as $str) {
	if (stripos($haystack,$str) !== false) {
		if ($behavior == 'any') return true;
	} else {
		if ($behavior == 'all') return false;
	}
}
return ($behavior == 'any' ? false : true);
}

// Implemented like so:
if (find_stuff(array($liveone,$livetwo,$livethree,$livefour,$livefive),$content,'any')) echo "Found it!";

?>

I am still getting an error.  Here is the entire code as I have it so far. Once it is working right I will move that function into the external functions file.

 

<?
include 'header.php';
echo "\r\n";
$nowtime = date('F d Y');
$currenttime = strtotime("$nowtime");
?>
<div class="borderwrap">
<div id="logostrip"><a href="<?PHP echo "$linkboturl"; ?>/"><img src="<?PHP echo
"$imagedir"; ?>/linkbot.png" alt="LinkBOT" style="vertical-align: top;" align="left" border="0"></a></div></div>
<table id="row2" width="100%" cellpadding="0" cellspacing="1" style="text-align:center;font-family:'Trebuchet MS',sans-serif;font-size:1.3em;">
<tr>
  <td class="row2" align="left">Welcome: <? echo "$admitname"; ?>
  </td>
  <td class="row2">LinkBOT Status:
  <?PHP
if ($lbstatus == 0)
{
    echo "<font color=\"#990033\"> Offline </font>";
}
else
{
    if ($lbstatus == 1)
    {
        echo "<font color=\"#00CC00\"><nobr> Online </nobr></font>";
    }
    else
    {
        echo "<font color=\"#firebrick\">Error</font>";
    }
}
?>
  </td>
  <td class="row2">
  LinkBOT Help
  </td>
</tr>
</table>
<table id="row2" width="100%" cellpadding="0" cellspacing="1" style="text-align:center;font-family:'Trebuchet MS',sans-serif;font-size:1.3em;">
<tr>
  <td class="row2" align="left">Full Auto
  </td>
  <td class="row2">Semi Auto
  </td>
  <td class="row2"><font color=#009900>Manual</font></a>
  </td>
</tr>
</table>
<form method=post action=test.php><input type=hidden name=startnow value=go>
<center><textarea name=linkinput cols=110 rows=5 style="background: #ffffff; border: 1px solid #000000;"></textarea><br>
<center><input type="checkbox" name="hidelive" value="Y" checked="checked" />Hide Live Links &nbsp &nbsp &nbsp <input type=submit value="Verify Links" style="background: #ffffff; font-weight: bold; color: #000000;"></center></form>
<?php
if (isset($_POST["startnow"]))
{
?>
<div id="row3">
	<div style="float: left;"><img src="<?PHP echo "$imagedir/nav_m.gif"; ?>" alt=">" border="0" height="8" width="8"><font color="#FFFFFF"> LinkBOT Version 2.5</font></div>
	<div align="right"><font color="#FFFFFF">Manual Mode</font></div>
</div>
<table class="table" cellspacing="1">
	<tbody><tr>
		<th width="50"><center>Status</center></th>
		<th><center>Link</center></th>
	</tr>
<?
    $linkinput = $_POST["linkinput"];
    $hidelive = $_POST["hidelive"];
    $linkinput = str_replace("\r\n", " ", $linkinput);
    $linkinput = str_replace("http", ". http", $linkinput);
    $linkinput = str_replace(")", " )", $linkinput);
    $Exploded = explode(" ", $linkinput);
    $Ttl_Parts = count($Exploded);
    for ($i = 0; $i <= $Ttl_Parts; $i++)
    {
        if (((@strpos($Exploded[$i], "http")) === false) || ($Exploded[$i] == "") || ($Exploded[$i] ==
            " "))
        {
            unset($Exploded[$i]);
        }
    }
    $Newlinks = array_values($Exploded);
    $Max_Parts = count($Newlinks);
    for ($i = 0; $i < $Max_Parts; $i++)
    {
        $filepath = $Newlinks[$i];
        if (@fclose(@fopen("$filepath", "r")))
        {
            if ($handle = @fopen($filepath, "r"))
            {
                $content = "";
                $part = fread($handle, 50);
                $content .= $part;
                if (eregi("</body>", $part))
                    break;
                fclose($handle);
            }
            if (preg_match("/<!DOCTYPE/i", $content) || preg_match("/<html>/i", $content))
            {
                $parsedhost = parse_url($filepath);
                $host = $parsedhost[host];

                mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
                mysql_select_db("$dbase");
                $query = "SELECT * FROM servers WHERE (server='$host')";
                $result = mysql_query($query) or ("" . mysql_error());
                while ($row = mysql_fetch_array($result))
                {
                    extract($row);
                }
                if (($ifExists = mysql_num_rows($result)) && $ifExists >= 0)
                {
                    if ($handle = @fopen($filepath, "r"))
                    {
                        $content = "";
                        while (!feof($handle))
                        {
                            $part = fread($handle, 1024);
                            $content .= $part;
                            if (eregi("</body>", $part))
                                break;
                        }
function find_stuff ($needles,$haystack,$behavior = 'all') {
// $behavior should either be 'all' or 'any'
// $needles is an array
// $haystack is a string

foreach ($needles as $str) {
	if (stripos($haystack,$str) !== false) {
		if ($behavior == 'any') return true;
	} else {
		if ($behavior == 'all') return false;
	}
}
return ($behavior == 'any' ? false : true);
}

if (find_stuff(array($liveone,$livetwo,$livethree,$livefour,$livefive),$content,'any'))       
	   			   {
                            if ($hidelive == Y)
                            {
                            }
                            else
                            {
                                echo "<tr><td id='row2' align='center'>";
                                if ($currenttime < $trialend)
                                {
                                    echo "<img src='$imagedir/check.gif'><img src='$imagedir/live.gif'>";
                                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                    flush();
                                }
                                else
                                {
                                    echo "<img src='$imagedir/live.gif'></td>";
                                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                    flush();
                                }
                            }
                        }
                        else
                        {
                            if (preg_match("/$deadone/", $content) || preg_match("/$deadtwo/", $content) ||
                                preg_match("/$deadthree/", $content) || preg_match("/$deadfour/", $content) ||
                                preg_match("/$deadfive/", $content))
                            {
                                if ($currenttime < $trialend)
                                {
                                    echo "<tr><td id='row2'><img src='$imagedir/check.gif'><img src='$imagedir/dead.gif'>";
                                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                    flush();
                                }
                                else
                                {
                                    echo "<tr><td id='row2' ><img src='$imagedir/dead.gif'></td>";
                                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                    flush();
                                }
                            }
                            else
                            {
                                echo "<tr><td id='row2'>Status: Error</td>";
                                echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                                flush();
                            }
                        }
                    }
                }
                else
                {
                    echo "<tr><td id='row2'>Status: This Server is not supported</td>";
                    echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                    flush();
                }
            }
            else
            {
                echo "<tr><td id='row2'>Status:<font color=green> This is a direct file link.  The file exists.</font></td>";
                echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
                flush();
            }
        }
        else
        {
            echo "<tr><td id='row2' >Status:<font color=green> This is a direct file link.  The file does not exist.</font></td>";
            echo "<td id='row2' align='left'>&nbsp &nbsp $Newlinks[$i]</td></tr>";
            flush();
        }

    }
}
echo "</table>";
include 'footer.php';
?>

 

The error I am getting is

Parse error: syntax error, unexpected T_ELSE in C:\Program Files\KeyFocus\KFWS\htdocs\test.php on line 159

 

This is line 159(The else is line 159. That is the only thing on that line)

  else
                        {
                            if (preg_match("/$deadone/", $content) || preg_match("/$deadtwo/", $content) ||
                                preg_match("/$deadthree/", $content) || preg_match("/$deadfour/", $content) ||
                                preg_match("/$deadfive/", $content))
                            {

 

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.