soma56 Posted June 8, 2010 Share Posted June 8, 2010 New to PHPFREAKS! I recently started learning php and so far I'm really enjoying it. I can see why it's addicting. 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 More sharing options...
bobbinsbro Posted June 8, 2010 Share Posted June 8, 2010 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 https://forums.phpfreaks.com/topic/204194-file_get_contents-question/#findComment-1069494 Share on other sites More sharing options...
soma56 Posted June 8, 2010 Author Share Posted June 8, 2010 Thank you for the quick reply. I will review the tutorials and let you know if I figured it out. Link to comment https://forums.phpfreaks.com/topic/204194-file_get_contents-question/#findComment-1069502 Share on other sites More sharing options...
soma56 Posted June 8, 2010 Author Share Posted June 8, 2010 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 https://forums.phpfreaks.com/topic/204194-file_get_contents-question/#findComment-1069524 Share on other sites More sharing options...
soma56 Posted June 8, 2010 Author Share Posted June 8, 2010 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 https://forums.phpfreaks.com/topic/204194-file_get_contents-question/#findComment-1069529 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.