Jump to content

Finding out which match shows up first


play_

Recommended Posts

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 />";
}
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/49860-finding-out-which-match-shows-up-first/
Share on other sites

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?!

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

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.

 

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 //" ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.