Page 1 of 2
How to find content on a page with PHP?
Posted: Sat Mar 04, 2006 5:54 pm
by JAB Creations
I'd like to build an extremely simple script that will be used in part of a larger script. This script will when correctly working echo if a string in the static HTML "<!--stuff-->" exists or not within the same file; thats it. I plan on erasing and reinserting this string from and back to the file. I'd like to keep this as simple as possible and since my files change a lot I need this to work with the static string we're looking if it's anywhere on the file (and not on say specifically line 7 or 23 or something predefined).
For simple reference, let's call our file "example.php". Both the script and the string we're looking for need to be part of "example.php".
example.php
<html><head><title></title></head>
<body>
<?php if () { echo 'found string';}
else () {echo'could not find string';}
<!--stuff-->
<p>stuff</p>
</body>
</html>
I'm sure this is going to deal with file command(s) and maybe some sort of _self syntax but this is why I'm asking.
John
Posted: Sat Mar 04, 2006 6:09 pm
by a94060
i dont seem to understand your question.
Posted: Sat Mar 04, 2006 6:16 pm
by JAB Creations
If "<!--stuff-->" exists echo 'it exists'.
If "<!--stuff-->" does not exist echo 'can\'t find'.
All this script is doing is looking for "<!--stuff-->" and letting us know if it's in the file or not.
Both the script and "<!--stuff-->" are on the same file (no multiple files).
John
Posted: Sat Mar 04, 2006 6:28 pm
by a94060
Code: Select all
if (!<!--stuff-->) {
echo 'This stuff that you are looking for,does not exist';
}
/*i think this is what you want. since i do not know how to search a php script via php,i cant tell how u can search a page.
This thing i wrote is,if whatever ur looking for does not exist,it will echo the does not exist*/
Posted: Sat Mar 04, 2006 6:38 pm
by JAB Creations
Not solved; I think you have JavaScript on your mind right now.
The method I need has to work in a larger script as an initial condition. I simply need the correct code to understand what syntax I need to use. Explaining the larger idea of the script this smaller script will fit in to will throw off this simple question of mine however.
Taking guesses isn't going to really help me as I need someone who knows PHP syntax to the extent that includes at least an idea of what syntax should probally be used (though there is so much syntax I've been browsing the PHP site for a good chunk of my day today). I'm sure it's some fsomething command but in a somewhat complicated setup.
John
Posted: Sat Mar 04, 2006 6:51 pm
by s.dot
Code: Select all
$file = file_get_contents("/some/file.ext");
if(preg_match("#somepatternthatyou'relookingfor#",$file)){
echo "string found";
} ELSE {
echo "string not found";
}
Posted: Sat Mar 04, 2006 6:52 pm
by a94060
scrotaye wrote:or use fopen() and loop through the file line by line until you find a match
thnaks 4 helping.i did not know what i wwas talking about,but i tried

Posted: Sat Mar 04, 2006 6:52 pm
by s.dot
or use fopen() and loop through the file line by line until you find a match
Posted: Sat Mar 04, 2006 7:09 pm
by JAB Creations
Thank you for trying a94060. If no one tried then we'd all just fall over and die.
scrotaye...
Well it's the right idea at least, but it's the classic problem of returning true when it's false. Remove "<!--stuff-->" and it will still say it found the string. I've seen this happen a few times in my other scripts but I'm simply not that skilled yet.
test.php
<html>
<head>
</head>
<body>
<!--stuff-->
<?php
$file = file_get_contents('test.php');
if(preg_match("<!--stuff-->",$file))
{echo "string found";}
else
{echo "string not found";}
?>
</body>
</html>
Posted: Sat Mar 04, 2006 7:14 pm
by a94060
that does not work? that should seem to work because u are trying to match <--stuff--> with preg_match,and u already read th efile contents,so that should work unless you are getting some error or soemthing.
Remember your PHP tags...Code: Select all
<html>
<head>
</head>
<body>
<!--stuff-->
<?php
$file = file_get_contents('test.php');
if(preg_match("<!--stuff-->",$file))
{echo "string found";}
else
{echo "string not found";}
?>
</body>
</html>
Posted: Sat Mar 04, 2006 7:27 pm
by s.dot
it's reading the < as your delimiter
use if(preg_match("#<!--stuff-->#",$file)){ was found } else { was not found }
Posted: Sat Mar 04, 2006 7:29 pm
by a94060
yea,that could be true. i dont think that jab will be searching for a comment in his file,so s/he probably wont need to worry about that right?
Posted: Sat Mar 04, 2006 8:16 pm
by feyd
Posted: Sat Mar 04, 2006 8:37 pm
by JAB Creations
Still not working.
Yes, I ~AM~ looking for a comment...the main script (not this one) will be looking for a comment to insert data afterwards.
Posted: Sat Mar 04, 2006 8:37 pm
by JAB Creations
Good idea feyd...but I have no clue how to implement it?