RegEx to get data between tags

Maxoq

Well-known member
Registered
Joined
Feb 25, 2015
Messages
520
Points
28
I want to use Regex in PHP to select all text or data between tags (HTML). What is the best way to select all the text between 2 tags, for instance, the text between all the '<div>' and </div> tags on the page or <div class="topcont"> and </div>. After got the deta I would to have it in an arrray.
 

Cort Ammon

Member
Registered
Joined
Jul 8, 2016
Messages
48
Points
8
Try this

Code:
<?php
$content = "<div class="topcont">your text here</div>";
$pattern = '<div class="topcont">(.+)((\s)+(.+))+</div>';
preg_match($pattern, $content, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
 

fwh

Administrator
Staff Member
Joined
Dec 8, 2012
Messages
773
Points
63
@Maxoq your question made me remember about a small code that I wrote 10 years ago, it can get content between html tags on whatever sites if wanted.
Here's codes that you can use to get data between tags

PHP:
<?php
header('Content-type: text/html; charset=utf-8');
function getcon($url) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_URL, $url);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;
}
//$i='';
$arr=array(
 //my array;
);
foreach($arr as $value){		
	$myFile = "$value.txt";	
	$urlGr=str_replace(" ","-",$value);
	$fh = fopen($myFile, 'w') or die("can't open file");
	$br='<br>';
	fwrite($fh, $br);
	$i=21;
	while($i<=186){	
		if($i==1)
		{$ap='';}
		else{
			$ap='page/'.$i.'/';
		}
		//$i = ($j * 16) - 16;
		$content=getcon("URL/$urlGr/$ap");
		//patern="[\s\S\w]*?";
		//$patern="[\s]*([\s\S\w]+?)";
		if(preg_match_all('~<td height="5" valign="top" class="titlestyle"><div align="center"><a href="[\s]*([\s\S\w]+?)" class="titlestyle"~i',$content,$matches)){
	//	  echo '<pre>';
	//	  print_r($matches[1]);
	//	  echo '</pre>';
		}
		$i=$i+1;
	    
	    foreach($matches[1] as $value){
	//	echo $value."<br>";
		$value=$value.$br;
		fwrite($fh, $value);
		}
	}
	fclose($fh);
}
	echo "OK";

?>
Note on this [\s]*([\s\S\w]+?), it will get data and assign into an array, after that you can display it and use it for any purposes.

Hope it helps!
 
Older Threads
Replies
4
Views
3,483
Replies
7
Views
3,950
Replies
2
Views
2,403
Latest Threads
Replies
1
Views
27
Replies
0
Views
192
Replies
1
Views
40
Replies
2
Views
83
Recommended Threads

Latest postsNew threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top