Page 1 of 2
[SOLVED] Testing for all occurrences of a string
Posted: Thu Jul 01, 2004 11:48 am
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.
Posted: Thu Jul 01, 2004 11:53 am
by feyd
[php_man]preg_match_all[/php_man]
Posted: Thu Jul 01, 2004 11:55 am
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.
Posted: Thu Jul 01, 2004 12:01 pm
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

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

Posted: Thu Jul 01, 2004 12:03 pm
by evilmonkey
No, it's what after the string that concerns me. For example
Hello Bob
Hello Mike
Hello Mary
See what I mean?
Posted: Thu Jul 01, 2004 12:10 pm
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 />';
}
?>
Posted: Thu Jul 01, 2004 12:16 pm
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.
Posted: Thu Jul 01, 2004 12:22 pm
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 />';
}
?>
Posted: Thu Jul 01, 2004 12:43 pm
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!
Posted: Thu Jul 01, 2004 12:48 pm
by markl999
preg_match_all(($vacallsign."[^ ]*"), $vatsimdata, $matches);
Try
preg_match_all('/('.$vacallsign.' .[^ ]*)/', $vatsimdata, $matches);
Posted: Fri Jul 02, 2004 1:14 pm
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.

Posted: Fri Jul 02, 2004 3:49 pm
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?
Posted: Fri Jul 02, 2004 4:28 pm
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
}}
//...
?>
Posted: Sat Jul 03, 2004 12:06 pm
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) {
ї0]=>
array(0) {
}
ї1]=>
array(0) {
}
}
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</a></td>
<td>KCMA&nbsp;</td>
<td align="center"><a href="javascript:map('MMSD','KLAX')">MMSD</a></td>
<td align="center"><a href="javascript:map('MMSD','KLAX')">KLAX</a></td>
<td><a href=""></a></td>
<td>USA-SE&nbsp;</td>
<td align="center">03/07/2004 00:12:40</td>
<td align="center">0:45:08</td>
</tr>
<tr><td><a href="">AAL138</a></td>
<td>KDFW&nbsp;</td>
<td align="center"><a href="javascript:map('KLBB','KABQ')">KLBB</a></td>
<td align="center"><a href="javascript:map('KLBB','KABQ')">KABQ</a></td>
<td><a href="">864839</a></td>
<td>USA-W&nbsp;</td>
<td align="center">03/07/2004 00:31:37</td>
<td align="center">0:26:11</td>
</tr>
<tr><td><a href="">AAL243</a></td>
Notice AAL243, AAL138, etc...
Ideas? Thanks.

Posted: Sat Jul 03, 2004 12:10 pm
by markl999
Ah, you can't just take a regex to match "hello fred" and use it to match AALxxx

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);