FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


preg_match multiline





chatrack
Hi,

I like to match 'All' contents including white space, tab, next line,etc.

between <div class="mini-title">and </div> and store in a $variable.



For instance
<div class="mini-title">contents....[next line]
contents..contents[spaces]....[tabs]...contents...
more conents...
<\/div>



for tha i tried preg_match, but it ended up in a single line.

preg_match(/'<div class="mini-title">(.*?)[\t\n]+/', $htmlcontent, $match)


Can you suggest a better regex?
Peterssidan
Code:
preg_match('/<div class="mini-title">(.*?)<\/div>/s', $htmlcontent, $match);
$variable = $match[1];
chatrack
Thanks for help. It worked fine Very Happy

Code:
<?php

$htmlcontent='<div class="mini-title">contents....[next line]
contents..contents  [spaces]....[tabs]  ...contents...
more       conents... </div>';


if(preg_match('/<div class="mini-title">(.*?)<\/div>/s', $htmlcontent, $match))
        {
echo($match[1]);
     }

?>
AftershockVibe
Just FYI, if you're using things like this a lot on your site then you might want to look at using an XML parser to extract particular data from named classes or IDs (assuming the site you're reading in follows a valid standard). This won't be worth the overhead for one off scrapings though.
jmraker
Code:
function getContentsBetween($str1, $str2, $string, $include=true){
   $pos = stripos($string, $str1);
   if($pos !== false){
      if(!$include)
         $pos += strlen($str1);
      $ret = substr($string, $pos);
      if($str2 == '*EOF'){
         $pos = strlen($ret);
         $str2 = '';
      }
      else
         $pos = stripos($ret, $str2);
      if($pos !== false){
         if($include)
            $pos += strlen($str2);
         return substr($ret, 0, $pos);
      }
   }
   return false;
}


I've made a function that does something like this. and you could use it like
Code:

$html = getContentsBetween('<div class="mini-title">', '</div>', $string, false);
if($html !== false){
  // $html contains the text between the 2 strings
}
chatrack
Thank you jmraker
Related topics
script backup database
php variables
Site Nav Suggestions...
flash
very simple PHP code to get search query string
Executing a .sql file through PHP
HOWTO?$lang files!
Echoing Titles
PHP DB Error: syntax error
problem with attached css style sheet
Server uptime tracker
PHP Preg_match() Help
a regex help
preg_match
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.