Jump to content

[SOLVED] Help with regex


therealwesfoster

Recommended Posts

When I run a preg_match, and there are multiple </b> (where i want the regex to stop), how can I make it stop at the NEXT occurrence? Sometimes it skips all the way to the last occurrence, or somewhere in between. IE:

 

 

Here's my sample code:

<h2>
<i>
<div>
  <b>
   <i id="2">My text!</i>
  </b>
</div>
</i>
</h2>

 

And my regex: (this is a quick throw-together.. non tested. just to help me ask my question)

 

<?php
$string = file_get_contents("THE_ABOVE_CODE");

$body = preg_match('/\<i id="2">(.*)\<\/i>/ism',$string,$match);

echo "<pre>".print_r($match)."</pre>";
?>

 

Now in the code, instead of selecting the </i> right after the text "My Text!", it selects the last </i> in the script... why?

 

Thanks

Link to comment
Share on other sites

What you're looking for is the laziness operator "?".

Here's how your regular expression should look like: (Just added the laziness operator)

 

$body = preg_match('/\<i id="2">(.*?)\<\/i>/ism',$string,$match);

 

Orio.

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.