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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.