play_ Posted May 3, 2007 Share Posted May 3, 2007 Say i have a paragraph. I am searching for either a " or ' or /* I have to use it with strpos(). So i'm doing this: $find = ' /* '; $bpos = strpos($paragraph, $find); if($bpos !== false) { echo "found $find at position: $bpos"; } What i'd like to do is something like this: $find = array("\*", "'", "\""); $bpos = strpos($paragraph, $find); if($bpos !== false) { echo "found $find at position: $bpos"; } Pretty much find any of the $find elements. without a loop though. i can't do this: <?php $paragraph = "hello /* world // comment here whatever"; $find = array("/*", "//"); $size = count($find); for($i = 0; $i <= $size; $i++) { $bpos = strpos($paragraph, $find[$i]); if($bpos !== false) { echo "found $find[$i] at position: $bpos <br />"; } } ?> Quote Link to comment Share on other sites More sharing options...
play_ Posted May 4, 2007 Author Share Posted May 4, 2007 Ok this was a failure. Then here's a maybe simpler/easier question: How can i make strpos() conitunue looking? for example: $text = "once @ upon a @ time"; $bpos = strpos($text, '@'); if($bpos !== FALSE) { echo "Found @ at position: $bpos"; } returns: Found @ at position: 5 I know strpos() returns the first occurance, but is there a way to make it continue looking for all occurances? maybe i could use a while loop to search until it reaches the end of the line?! Quote Link to comment Share on other sites More sharing options...
play_ Posted May 4, 2007 Author Share Posted May 4, 2007 Ok i made a recursive function, like so: function test($text, $pos) { $pos = strpos($text, '@', $pos+1); if($pos !== FALSE) { echo "Found @ at position: $pos <br />"; test($text, $pos); } } test($text, -1); But i don't like this method. if anyone know of a better one, please share Quote Link to comment Share on other sites More sharing options...
taith Posted May 4, 2007 Share Posted May 4, 2007 not sure if it'd let you do this... but i think it'd work... function test($text){ $offset=0; while($pos=strpos($text, '@', $offset)!==false){ $out[]=$pos; $offset=$pos+1; } return $out; } print_r(test($text)); Quote Link to comment Share on other sites More sharing options...
play_ Posted May 4, 2007 Author Share Posted May 4, 2007 Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 16 bytes) in D:\sites\del2.php on line 15 line 15: while($pos=strpos($text, '@', $offset)!==false){ anyways, what about the original post? I'm gonna give another example, maybe it wasnt explained too well. I'm making a syntax hilighter and, im searching for Strings, Comments and Keywords. So say i have this, for example: $kw = "function"; $comment = "/*" $comment_close = '*/'; $string = '"'; Now, i need to search the code for whatever comes first, so i can decide which action to perform. So say it finds a comment (/*). then i can Start the hiliting there and skip searching for a $kw or string until $comment_close is found. Quote Link to comment Share on other sites More sharing options...
play_ Posted May 4, 2007 Author Share Posted May 4, 2007 Almost as if i was trying to do this: $pos = strpos($text, '/* OR " OR function OR # OR //'); Or maybe it can be done with regular expressions? like preg_match($pattern, $subject); where $pattern could be /* or # or ' can a pattern be that? instead of a pattern being "#" it would be "# or //" ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.