Page 1 of 1

Reading Characters

Posted: Sat Jan 31, 2004 5:13 pm
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

Posted: Sat Jan 31, 2004 6:35 pm
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.

Posted: Sun Feb 01, 2004 1:02 pm
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

Posted: Tue Feb 03, 2004 2:12 pm
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.

Posted: Tue Feb 03, 2004 3:44 pm
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 :)

Posted: Tue Feb 03, 2004 3:59 pm
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";
}
?>

Posted: Tue Feb 03, 2004 5:11 pm
by d3ad1ysp0rk
$userdatabase = "0";

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


how would it have a name in it if it's just a "0"??

Posted: Tue Feb 03, 2004 5:43 pm
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

Posted: Wed Feb 04, 2004 5:15 am
by desinc
Err.. :oops:

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