[SOLVED] Testing for all occurrences of a string
Moderator: General Moderators
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
[SOLVED] Testing for all occurrences of a string
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.
Thanks!
~evilmonkey.
Last edited by evilmonkey on Sat Jul 03, 2004 12:18 pm, edited 2 times in total.
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
I'm confusedI 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.
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
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
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 />';
}
?>- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
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.
Thanks for the help.
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 />';
}
?>- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
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:
A tip is appreceated. Thanks!
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);- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
No, still no luck. Here's the modified code:
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.
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;}}
//...Still no luck here.
Need to debug it then. After this line:
preg_match_all('/('.$vacallsign.' .[^ ]*)/', $vatsimdata, $matches);
Do:
What does that output?
preg_match_all('/('.$vacallsign.' .[^ ]*)/', $vatsimdata, $matches);
Do:
Code: Select all
echo $vatsimdata.'<br />';
echo '<pre>';
var_dump($matches);
echo '</pre>';- johnperkins21
- Forum Contributor
- Posts: 140
- Joined: Mon Oct 27, 2003 4:57 pm
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
}}
//...
?>- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
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):
I assume this means there are no results? The value of $vacallsign is AAL, this is the complete code:
$vatsimdata does in fact contain the string "AAL" as shown here:
Notice AAL243, AAL138, etc...
Ideas? Thanks.
Code: Select all
array(2) {
ї0]=>
array(0) {
}
ї1]=>
array(0) {
}
}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; }
?>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>Ideas? Thanks.