How to find content on a page with PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

How to find content on a page with PHP?

Post 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
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

i dont seem to understand your question.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post 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*/
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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";
}
Last edited by s.dot on Sat Mar 04, 2006 6:52 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post 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 :oops:
Last edited by a94060 on Sat Mar 04, 2006 6:55 pm, edited 2 times in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

or use fopen() and loop through the file line by line until you find a match
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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>
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post 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>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

it's reading the < as your delimiter

use if(preg_match("#<!--stuff-->#",$file)){ was found } else { was not found }
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

strpos() anyone?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Good idea feyd...but I have no clue how to implement it?
Post Reply