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

Link to comment
Share on other sites

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 />";
}

Link to comment
Share on other sites

Oops, forgot to add the before_needle parameter:

 

<?PHP
//match and find the color
$searchstring = "red";

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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