Thread: Regex help?
View Single Post
Old 02-09-2010, 09:17 PM   #4 (permalink)
delayedinsanity
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Regular expressions are fairly easy to pick up, they're just a little harder to master.

php Code:
header( 'Content-type: text/plain' );

$string = '<html> <test> Some content {gallery=/path/to/images} some more {gallery} </test> </html>';

// This won't work, because if there's more than one, it will get greedy
// $string = preg_replace( '~{gallery=(.*)}~i', 'FOUND ONE!', $string );

// This only matches up to the first brace it finds
$string = preg_replace( '~{gallery=([^}]+)}~i', 'FOUND ONE!', $string );

echo $string;

The above is just an example of one way to improve your expression, I'll leave the insertion of the gallery to you since you seem to have a handle on it. Uncomment the first one using the original pattern (and comment the second) and you'll see what I'm referring to.

.* is powerful, but almost always misused. Try to avoid it while you're learning and you'll pick up tricks that will help you build much more complex patterns down the road.
delayedinsanity is offline  
Reply With Quote