Jump to content


Photo

parsing strings


  • Please log in to reply
7 replies to this topic

#1 hellonoko

hellonoko
  • Members
  • PipPipPip
  • Advanced Member
  • 213 posts

Posted 28 March 2006 - 02:02 PM

I need to go through a list of words and find ones with certain letters in the last two places.

I know how to code this but am not sure of what str functions I would use.

I am looking around a little in the PHP manual can anyone point me in the right direction?


Also my host does not support the php5 fuction stream_get_contents()

What would be an alternative function for getting the contects of a HTML page and processing them in my php page.

Thanks in advance.

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 28 March 2006 - 02:08 PM

How is your list of words setup? Is it an array? Is it all in a string? I'd use the substr() function to look at the words and determine if it matches your pattern.

I would use fopen() for that, but I'm going to guess that you're not allowed to use that function either... but maybe.

Info: PHP Manual


#3 hellonoko

hellonoko
  • Members
  • PipPipPip
  • Advanced Member
  • 213 posts

Posted 28 March 2006 - 02:14 PM

[!--quoteo(post=359281:date=Mar 28 2006, 10:08 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 28 2006, 10:08 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
How is your list of words setup? Is it an array? Is it all in a string? I'd use the substr() function to look at the words and determine if it matches your pattern.

I would use fopen() for that, but I'm going to guess that you're not allowed to use that function either... but maybe.
[/quote]


the list of word will be put into an array.

does fopen() work for remote files and urls as well as files on the server?



#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 28 March 2006 - 02:16 PM

<?php
for($i = 0; $i < count($myarray); $i++)
{
     if(substr($myarray[i], -2) == 'ab')
          // do something
}

And yes, fopen() opens both remote and local files.

Info: PHP Manual


#5 hellonoko

hellonoko
  • Members
  • PipPipPip
  • Advanced Member
  • 213 posts

Posted 28 March 2006 - 02:32 PM

[!--quoteo(post=359286:date=Mar 28 2006, 10:16 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 28 2006, 10:16 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
<?php
for($i = 0; $i < count($myarray); $i++)
{
     if(substr($myarray[i], -2) == 'ab')
          // do something
}

And yes, fopen() opens both remote and local files.
[/quote]


when i use fopen() to open a URL i get only:

Resource id #4

I asume it is opening the page but not putting the acctually contents into my varriable.



#6 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 28 March 2006 - 03:42 PM

Exactly. That's because fopen() returns a file handle number. You need to use fread() to actually read the contents of the file.

You could also use fgets() in a loop.

Info: PHP Manual


#7 hellonoko

hellonoko
  • Members
  • PipPipPip
  • Advanced Member
  • 213 posts

Posted 28 March 2006 - 04:03 PM

[!--quoteo(post=359315:date=Mar 28 2006, 11:42 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 28 2006, 11:42 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Exactly. That's because fopen() returns a file handle number. You need to use fread() to actually read the contents of the file.

You could also use fgets() in a loop.
[/quote]


i had to use file_get_contents()

but i got it working.

thanks!

#8 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 28 March 2006 - 04:05 PM

Yeah... that'll work too... I forgot about that function.

Info: PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users