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.

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.