Jump to content

match must have xyz and abc but in no particular order


msaspence

Recommended Posts

how would i match a string that contains xyz and abc but in no particular order

 

for example

 

both

aslkdjf abc kasjdfakj xyz adflkjadsf

and

aslkdjf xyz kasjdfakj abc adflkjadsf

 

would match

 

i know that in this example i could use | with both combinations

but if i wanted to expand this to match xyz, abc, mno and qrs the number of combinations would be huge

 

 

 

Link to comment
Share on other sites

My suggestion would be to create an array of the string segments you want to match it to, then search for each individually, then all of them have to evaluate true for it to be a valid string.

 

A CRUDE ALGORITHM:

 

HAYSTACKSTRING = "BLAH BLAH BLHA ABC XYZ"

SEARCHSTRINGS = ARRAY ("ABC", "XYZ")

GOODSTRING = TRUE

FOREACH (SEARCHSTRING AS NEEDLESTRING) {

      IF ( NOT (NEEDLESTRING FOUND IN HAYSTACK STRING)) {

            GOODSTRING = FALSE

      }

}

IF (GOODSTRING) {

      WHATEVER YOU WANT TO DO TO IT

}

Link to comment
Share on other sites

I think the pipe is your only option, short of running a bunch of strstrs. The advantage of using the pipe is the ability to condense similar strings. For example, if you were looking for "abc" and "abd" you would use /(?:ab[cd]|...more stuff...)/ rather than /(?:abc|abd|...more stuff...)/.

Link to comment
Share on other sites

prehaps if i give a more precise example

 

i want to find a certain tag with a cetain id, class and title

 

so i might use

 

/<div id="mydiv" class="myclass" title="mytitle".*>[^<]*</div>

 

but this requires the id class and title attribute to be in a specified order which they might not nessassarily be

 

Link to comment
Share on other sites

See below. If you're doing a replace, you can use preg_replace_callback to analyze the attributes instead.

 

<pre>
<?php
$data = <<<DATA
### Good
<div id="mydiv" class="myclass" title="mytitle">data</div>
<div title="mytitle" id="mydiv" class="myclass">data</div>
<div class="myclass" title="mytitle" id="mydiv">data</div>
### Bad
<div id="bob" class="myclass" title="mytitle">data</div>
<div title="main" id="mydiv" class="myclass">data</div>
<div class="math" title="mytitle" id="mydiv">data</div>
DATA;

preg_match_all('%<div\s+(??:class="myclass"|id="mydiv"|title="mytitle")\s*){3}>%', $data, $matches);
foreach ($matches[0] as &$match) {
	$match = htmlspecialchars($match);
}
print_r($matches);
?>
</pre>

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.