[SOLVED] Testing for all occurrences of a string

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

User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

[SOLVED] Testing for all occurrences of a string

Post by evilmonkey »

Hello. I wish to test for all possible occurrences of a string. I know strstr(), but that finds the first occurrence of the string, I need all of them. Is there a function that can do that? I will also need to be able to cycle through the results using a loop to output them.

Thanks!

~evilmonkey.
Last edited by evilmonkey on Sat Jul 03, 2004 12:18 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]preg_match_all[/php_man]
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I don't really understand. Why does it return a two dimensional array?

EDIT: On second thought, I don't get the function altogether. Can you give an expample please.

Thanks.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I wish to test for all possible occurrences of a string. I know strstr(), but that finds the first occurrence of the string, I need all of them. Is there a function that can do that? I will also need to be able to cycle through the results using a loop to output them.
I'm confused :o
If you are searching for, lets say, "hello" then why do you need all of them, and need to loop through them ... as they will all just be "hello". Usually you just want to know if a string occurs and how many times .. i can't imagine why you'de need to loop over an array all containing the same string :o
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

No, it's what after the string that concerns me. For example

Hello Bob
Hello Mike
Hello Mary

See what I mean?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Ah..so like this for example?

Code: Select all

<?php
$string = 'one two hello bob three hello jane four five six hello fred seven';
preg_match_all('/(hello .[^ ]*)/', $string, $matches);
echo '<pre>';
var_dump($matches);
echo '</pre>';
foreach($matches[1] as $match){
  echo $match.'<br />';
}
?>
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I guess so, but I don't understand a couple of things. First off, why is it "'/(hello .[^ ]*)/'"? $matches from what I understand are the index of the matches that occurred and $string is the haystack, right? What does echo '<pre>'; var_dump($matches); echo '</pre>'; do? And finally, this would only outpu the "Hello"'s, wouldn't it? (echo $match?)

Thanks for the help.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

okie, here it is again with some comments. But regular expressions have entire books dedicated to them so i can't go into too much detail on the exact syntax, there's about 100 ways to do this another way too ;)

Code: Select all

<?php
//a test string
$string = 'one two hello bob three hello jane four five six hello fred seven';
//the below search $string and puts the results in $matches
//(hello .[^ ]*) means ...
//search for the word hello, followed by a space, then..
//any character that isn't a space .. that's the .[^ ]* bit
preg_match_all('/(hello .[^ ]*)/', $string, $matches);
//the pre/var_dump thing below just gives you a 'raw' view of the $matches array
//so you can see what's in it
echo '<pre>';
var_dump($matches);
echo '</pre>';
//this loops over all the matches it found (which will be held in $matches[1])
foreach($matches[1] as $match){
  echo $match.'<br />';
}
?>
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Hmmm...I see the logic, but this code isn't working. It gives me a blank image with no text, when I know there should be text. Here's the code:

Code: Select all

$vatsimdata = file_get_contents("page_on_the_internet");
//...
elseif ($typeofinfo == "vainfo") {
	//check how many pilots, if any, are online for the VA
	preg_match_all(($vacallsign."[^ ]*"), $vatsimdata, $matches);
	foreach ($matches[1] as $match) {
		$col = 75;
		imagestring ($newimage, 3, 3, $col, $match, $black);
		$col=$col+10;
               }
}
header("Content-type: image/png");
imagepng($newimage);
imagedestroy($newimage);
A tip is appreceated. Thanks!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

preg_match_all(($vacallsign."[^ ]*"), $vatsimdata, $matches);
Try
preg_match_all('/('.$vacallsign.' .[^ ]*)/', $vatsimdata, $matches);
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

No, still no luck. Here's the modified code:

Code: Select all

//...
elseif ($typeofinfo == "vainfo") {
	//check how many pilots, if any, are online for the VA
	preg_match_all('/('.$vacallsign.' .[^ ]*)/', $vatsimdata, $matches);
	foreach ($matches[1] as $match) {
		$col = 75;
		imagestring ($newimage, 3, 3, $col, $match, $black);
		$col=$col+10;}}
//...
It generates the image, with the rest of the data in there, but this particular data is not showing up, when there are about 5 instances of $vacallsign. The image is 270x110 pixels, and is a .png.

Still no luck here. :(
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Need to debug it then. After this line:
preg_match_all('/('.$vacallsign.' .[^ ]*)/', $vatsimdata, $matches);

Do:

Code: Select all

echo $vatsimdata.'<br />';
echo '<pre>';
var_dump($matches);
echo '</pre>';
What does that output?
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

Kind of off of your question, but...

Code: Select all

<?php
//...
elseif ($typeofinfo == "vainfo") {
   //check how many pilots, if any, are online for the VA
   preg_match_all('/('.$vacallsign.' .[^ ]*)/', $vatsimdata, $matches);
   foreach ($matches[1] as $match) {
      $col = 75; 
      imagestring ($newimage, 3, 3, $col, $match, $black); //$col always equals 75 here
      $col=$col+10; //This only makes $col = 85 after the last instance
}}
//... 
?>
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

John, thank you for the modification, this is why I always like to get others to check my code. Unfortunatly, that wasn't my problem. This is the result of debugging (as Mark suggested):

Code: Select all

array(2) &#123;
  &#1111;0]=&gt;
  array(0) &#123;
  &#125;
  &#1111;1]=&gt;
  array(0) &#123;
  &#125;
&#125;
I assume this means there are no results? The value of $vacallsign is AAL, this is the complete code:

Code: Select all

<?php
$vatsimdata = file_get_contents("http://website_edited_out");	
$vacallsign="AAL";
preg_match_all('/('.$vacallsign.' .[^ ]*)/', $vatsimdata, $matches);
echo '<pre>'; 
var_dump($matches); 
echo '</pre>';
foreach ($matches[1] as $match) {
	echo $match; }
?>
$vatsimdata does in fact contain the string "AAL" as shown here:

Code: Select all

...
AAL109&lt;/a&gt;&lt;/td&gt;
                          &lt;td&gt;KCMA&amp;nbsp;&lt;/td&gt;
                          &lt;td align="center"&gt;&lt;a href="javascript:map('MMSD','KLAX')"&gt;MMSD&lt;/a&gt;&lt;/td&gt;
                          &lt;td align="center"&gt;&lt;a href="javascript:map('MMSD','KLAX')"&gt;KLAX&lt;/a&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;a href=""&gt;&lt;/a&gt;&lt;/td&gt;
                          &lt;td&gt;USA-SE&amp;nbsp;&lt;/td&gt;
                          &lt;td align="center"&gt;03/07/2004 00:12:40&lt;/td&gt;
                               &lt;td align="center"&gt;0:45:08&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt;&lt;a href=""&gt;AAL138&lt;/a&gt;&lt;/td&gt;
                          &lt;td&gt;KDFW&amp;nbsp;&lt;/td&gt;
                          &lt;td align="center"&gt;&lt;a href="javascript:map('KLBB','KABQ')"&gt;KLBB&lt;/a&gt;&lt;/td&gt;
                          &lt;td align="center"&gt;&lt;a href="javascript:map('KLBB','KABQ')"&gt;KABQ&lt;/a&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;a href=""&gt;864839&lt;/a&gt;&lt;/td&gt;
                          &lt;td&gt;USA-W&amp;nbsp;&lt;/td&gt;
                          &lt;td align="center"&gt;03/07/2004 00:31:37&lt;/td&gt;
                               &lt;td align="center"&gt;0:26:11&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt;&lt;a href=""&gt;AAL243&lt;/a&gt;&lt;/td&gt;
Notice AAL243, AAL138, etc...

Ideas? Thanks. :)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Ah, you can't just take a regex to match "hello fred" and use it to match AALxxx :o
What is it you're now trying to match exactly? Is is all those AALxxxx values? If so then try:
preg_match_all('/('.$vacallsign.'[0-9]*)/', $vatsimdata, $matches);
Post Reply