Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Strpos not working with file
Old 02-18-2011, 02:41 AM Strpos not working with file
Junior Talker

Posts: 2
Name: No
Trades: 0
Hey Guys,
I have a simple one for you, if you could help me out with this, I would be VERY grateful because its driving me nuts!

So I am trying to search a file for a word. Thats it, not trying to write to the file, just read and search. I have simplified the code as much as possible and it still cant find it. Here is the code:



Code:
<?
$file = file_get_contents("users/crimsonlung.txt");
if(!strpos($file, "word")) {
echo "string not found!";
}
?>
So this should be looking into a file called "crimsonlung.txt" in the users folder for the text "word."

in the file, I have nothing but the text: "word" in it. So what is the problem?

I've echoed the variables just to make sure it was searching in the right spot and finding the data, and it was. So why is "string not found" being echod' each time? What could possibly be wrong?

Is there any other code I could use?

Thanks for any help!
crimsonlung is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-18-2011, 09:59 AM Re: Strpos not working with file
racer x's Avatar
Ultra Talker

Posts: 457
Name: Randy
Location: Northern Wisconsin
Trades: 0
(From the php manual)

Try writing it out as below. Note the use of exactly equal to "==="
You can alternately use "!=="

PHP Code:
$mystring 'abc';
$findme   'a';
$pos strpos($mystring$findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo 
"The string '$findme' was not found in the string '$mystring'";
} else {
    echo 
"The string '$findme' was found in the string '$mystring'";
    echo 
" and exists at position $pos";

http://php.net/manual/en/function.strpos.php
racer x is offline
Reply With Quote
View Public Profile Visit racer x's homepage!
 
Old 02-18-2011, 11:47 AM Re: Strpos not working with file
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
You can also use stripos to make the search case insensitive.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-18-2011, 11:19 PM Re: Strpos not working with file
shivaji's Avatar
Ultra Talker

Posts: 321
Trades: 0
Like racer x post === and !== is better solutions then !strpos.
__________________

Please login or register to view this content. Registration is FREE
- uncommon free scripts

Please login or register to view this content. Registration is FREE
- Städte, Sport, Party, Gourment, Apartments, Hotels
shivaji is offline
Reply With Quote
View Public Profile Visit shivaji's homepage!
 
Reply     « Reply to Strpos not working with file
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.08257 seconds with 12 queries