Jump to content

file_get_contents - question


soma56

Recommended Posts

New to PHPFREAKS! I recently started learning php and so far I'm really enjoying it. I can see why it's addicting.  :D

 

My question has to do with the 'file_get_contents' string. I know how to get and display the contents of a file:

 

$file = file_get_contents('./hello.txt');
echo $file;

 

But lets say I wanted to get and compare a specific value on the page that I'm getting:

 

hello.txt

I'm new to PHPFreaks and like what I've seen to far. My favorite color is [color=red][b]XXX[/b][/color]

 

The question is how would someone go about getting the value of "XXX"? If someone could point me in the right direction I'd greatly appreciate it!

Link to comment
https://forums.phpfreaks.com/topic/204194-file_get_contents-question/
Share on other sites

regular expressions

http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax

or if the string inside which the substring you want is static (always the same) you could use strcmp (case sensitive), strcasecmp (case insensitive), substr, strstr or something similar (chack out the functions on this page http://us2.php.net/manual/en/book.strings.php.

Thanks for the direction and I've figured it out :)

 

home.php

I'm new to PHPFreaks and like what I've seen to far. My favorite color is red

 

For my purposes this works.

 

$text = file_get_contents("hello.php");

echo $text;
					   
//match and find the color
$searchstring = "red";

if(stristr($text, $searchstring)) {
echo "string not found <br /><br />" ;
} else {
echo "string found <br /><br />";
}

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.