Reading Characters

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

Post Reply
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Reading Characters

Post by desinc »

I've just moved from Vbasic to PHP. At the moment I am trying to make a script that will check to see if a '#' is in a variable. Basically I'm looking for a function like fgetc but for variables.

Any help would be greatly appreciated.

Desinc
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

The function you're looking for is

Code: Select all

strstr($what_youre_looking_for,$what_youre_looking_in)
That will return true/false if the string was found/not found.


Go to http://ca2.php.net/strings for many more useful string functions.
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

Thanks. However, the two varabiles are the wrong way round. So the correct form is...

strstr($what_youre_looking_in,$what_youre_looking_for)

Thank you
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

Instead of creating a new topic I'd thought I'd reply in this one again...

How would I go about using strstr to read though a file? I've typed using file and file_get_contents to put the file into a varable but when I echo the varabile all I get is a 0.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

File_get_contents will put the file into a single string. File will put each line in a new element in an array. If you're just getting '0', I would imagine it's a result of a read in error. Maybe post some code so I can make a more educated guess :)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

Fine.. you asked for it. ;p

Code: Select all

<?php

//Delcare Varables
$userdatabase = "0";
$newaccount = "0";
//Delcare Varables
echo $userdatabase; //For Debugging 
$userdatavase=  file_get_contents("userdatabase.txt");
echo $userdatabase; //For Debugging
if (strstr($userdatabase,$_POST["requsername"] ))
{
echo "The username" . $_POST["requsername"] . "has already been taken";
}
?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

$userdatabase = "0";

if (strstr($userdatabase,$_POST["requsername"] ))


how would it have a name in it if it's just a "0"??
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Not sure if it was intentional, but "$userdatabase" is spelled wrong in this line:

Code: Select all

<?php
$userdatavase=  file_get_contents("userdatabase.txt"); 
?>
Probably should be:

Code: Select all

<?php
$userdatabase =  file_get_contents("userdatabase.txt"); 
?>
Which would explain why it echos "0" :D
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

Err.. :oops:

Yeah, I did mean it to be that, I mean.... thanks.
Post Reply