Jump to content

Get everything between this:


iHack

Recommended Posts

Regexps are best used when you're matching a specific pattern, which means you don't know the characters in advance.  In this case, you know the exact starting and ending points of the string, so I'd recommend a combination of strpos and substr to accomplish what you need.

 

<?php
  $srch1 = '<textarea name="textarea" rows="5">';
  $srch2 = '</textarea>';
  $pos1 = strpos($the_string, $srch1);
  if($pos1 !== FALSE){
    $pos1 += strlen($srch1);
    $pos2 = strpos($the_string, $srch2, $pos1);
    if($pos2 !== FALSE){
      $substr = substr($the_string, $pos1, $pos2 - $pos1);
      echo $substr;
    }
  }
?>

That may be slightly faster than invoking the regexp engine.  Also, I've had trouble in the past using regexps on very large strings so for things that could be potentially large I try to avoid them.

 

I didn't test that code, some of the offsets may be off by 1 so add '+ 1' as necessary.

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.