Jump to content

Merge two expressions with different mods


Lumio

Recommended Posts

Perhaps one suggestion could be?

 

$str = 'a little string with {$var} in it.
a little string with <<foo

bar >> in it';

preg_match_all('#((?:{|<<)[^}>]+(?:}|>>))#s', $str, $matches);
$matches[1] = str_replace("\r\n", '', $matches[1]); // clear out any newlines and such...
echo '<pre>'.print_r($matches[1], true);

 

Due to < and >, you'll have to view source to see the full results from the array.

 

Output:

 

<pre>Array
(
    [0] => {$var}
    [1] => <<foobar >>
)

First, sorry for the s modifier in my version... realized afterwards that since I'm not using a dot match all, it's not needed.. my bad. (and I forgot about adding the dollar sign in my opening alternation...):

#((?:{\$|<<)[^}>]+(?:}|>>))#

This code also assumes that if something starts with {$, it will finish with } to match {$something}.

 

Thank you :)

I made it a little bit different now:

/\({\$[\w\d\[\]^\n]+\})|(\<\<.+?\>\>)/

 

I don't think this will work..after your opening delimiter, you escape the first bracket \(, which means you are looking for a literal bracket in your string.. then your character class is not correct.. what this is saying is, look for any word character, or digit, or [ or ] or ^ or newline one or more times, then a \} (which you don't need to escape).

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.