  | 
              
	              
	                
  
    
	 
        preg_match_all() ค้าง - รบกวนตรวจสอบโค้ดชุดนี้ และแนะนำวิธีแก้ไขให้หน่อยครับ     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 html ไม่เหมาะกับ regular expression ครับ https://stackoverflow.com/a/1732454/128761 
 
Code (PHP) 
<?php
$str = '
<style>
.table th.fit,
.table td.fit {
	white-space: nowrap;
	width: 2%;
}
</style>
<div class="card">
	<div class="card-header bg-primary">
		<h3 class="card-title"><i class="fa fa-clipboard"></i> รายละเอียด <b>Reward</b></h3>
	</div>
	
	<div class="card-body">
		<table class="table table-bordered table-hover">
			<thead class="well">
				<tr>
					<th class="text-right fit">หัวข้อ</th>
					<th>ข้อมูล</th>
				</tr>
			</thead>
			<tbody parser-repeat="master_data">
				
				<tr>
					<td class="text-right fit"><b>Id :</b></td>
					<td>{id}</td>
				</tr>
				<tr>
					<td class="text-right fit"><b>รหัสลูกค้า :</b></td>
					<td>{member_code}</td>
				</tr>
				<tr>
					<td class="text-right fit"><b>แต้มสะสม :</b></td>
					<td>{reward_point}</td>
				</tr>
				<tr>
					<td class="text-right fit"><b>1=ครบ (10 แต้ม แลก 1 แก้ว) :</b></td>
					<td>{redemption}</td>
				</tr>
				<tr>
					<td class="text-right fit"><b>Create Datetime :</b></td>
					<td>{create_datetime}</td>
				</tr>
				<tr>
					<td class="text-right fit"><b>Create User Id :</b></td>
					<td>{create_user_id}</td>
				</tr>
				<tr>
					<td class="text-right fit"><b>Modify Datetime :</b></td>
					<td>{modify_datetime}</td>
				</tr>
				<tr>
					<td class="text-right fit"><b>Modify User Id :</b></td>
					<td>{modify_user_id}</td>
				</tr>
			</tbody>
		</table>
	</div>
</div>';
echo '<h3>original</h3>'.PHP_EOL;
echo '<pre>'.htmlspecialchars(print_r($str, true)).'</pre>'.PHP_EOL;
echo '<hr>'.PHP_EOL;
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = false;
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $str, LIBXML_HTML_NODEFDTD);
$table = $dom->getElementsByTagName('table')->item(0);
if (is_object($table)) {
	$tbody = $table->getElementsByTagName('tbody')->item(0);
	if ($tbody->hasAttribute('parser-repeat') && $tbody->getAttribute('parser-repeat') === 'master_data') {
		foreach ($table->getElementsByTagName('tr') as $tr) {
			if (is_object($tr)) {
				foreach ($tr->getElementsByTagName('td') as $td) {
					echo $td->nodeValue;
					echo '<br>'.PHP_EOL;
				}
			} else {
				throw new \Exception('Invalid html content (tr).');
			}
			echo '<!--end tr--><br>'.PHP_EOL;
		}
	} else {
		throw new \Exception('Invalid html content (tbody contain no attribute or wrong value for this attribute.).');
	}
	unset($tbody);
} else {
	throw new \Exception('Invalid html content (table).');
}
unset($table);
 
 
คงเอาไปประยุกต์ใช้เองได้นะครับ ผลมันออกมาเรียงให้แล้วล่ะ.                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-09-22 16:36:50              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-09-22 16:30:41 | 
                        By :
                            mr.v | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ขอบคุณสำหรับตัวอย่างโค้ดครับ 
 
แต่เนื่องจากรูปแบบที่ต้องการคืออาร์เรย์ที่มีข้อมูลดังในภาพนี้ ก็เลยต้องใช้ preg_match ครับ 
 
  
 
กับ HTML อีกชุด ก็ทำงานได้ปกติ 
 
Code (PHP) 
<?php
function getTextBetweenTags($string, $tagname) {
    $tag = '\w+';
    $attr = 'parser-repeat';
    
    $attr = preg_quote($attr);
	$value = preg_quote($tagname);
    
    $pattern = "/<(".$tag.")[^>]*".$attr."\s*=\s*".
					"(['\"])$value\\2[^>]*>((.|\s)+?)<\/\\1>/";
    preg_match_all($pattern, $string, $matches);
	
    return $matches;
}
$str = '<html>
	<head>
		<title>การแสดงผลแบบใช้ Parser Repeat</title>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
	</head>
    <body>
        <h3>{blog_heading}</h3>
		<ul>
			<li parser-repeat="data_list">{title}</li>
		</ul>
		<H1>One</h1>
		<table class="table table-bordered" border="1" style="width:500px">
			<thead>
				<tr bgcolor="#aaaaaa">
					<td>Title</td>
					<td>body</td>
				</tr>
			</thead>
			<tbody>
				<tr parser-repeat="blog_entries">
					<td>{title}</td>
					<td>{body}</td>
				</tr>
			</table>
		</table>
		<H1>Two</h1>
		<table class="table table-bordered" border="1" style="width:500px">
			<thead>
				<tr bgcolor="#aaaaaa">
					<td>Title 2</td>
					<td>body 2</td>
					<td>Link 2</td>
				</tr>
			</thead>
			<tbody parser-repeat="master_data">
				<tr>
					<td>{title}</td>
					<td>{body}</td>
					<td>{{link}}</td>
				</tr>
			</tbody>
		</table>
    </body>
</html>';
$txt = getTextBetweenTags($str, 'master_data');
echo '<pre>', htmlentities(print_r($txt,true)),'</pre>';
?>
 
 
ตอนนี้ก็เลยงง ว่าเกิดจากอะไรทำไมถึงรันกับ HTML ชุดแรกแล้วค้างไปเลยครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-09-22 22:06:29 | 
                        By :
                            {Cyberman} | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ปิดเออเร่อร์ไว้หรือเปล่า ดูใน log php-error เพื่อจะรู้ว่าอะไรเป็นอะไร                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-09-22 22:34:42 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
/**
 * print out string as html encoded using `htmlspecialchars()` function.
 */
function printDebug($string)
{
	echo '<pre>' . htmlspecialchars(print_r($string, true), ENT_QUOTES) . '</pre>' . PHP_EOL;
}
echo '<h3>original</h3>' . PHP_EOL;
printDebug($str);
echo '<hr>' . PHP_EOL;
/**
 * Get inner content of HTML table.
 * 
 * @param string $string
 * @param string $getElementName The HTML element name to get.
 * @param string|false $requiredAttribute Set to false for not validate, set the attribute name to validate.
 * @param string|false $requiredAttributeValue Set to false for not validate, set the attribute value to validate.
 * @return string
 * @throw exception on failed to validate or get HTML.
 */
function getHTMLTableInnerContent($string, $getElementName = 'tbody', $requiredAttribute = false, $requiredAttributeValue = false)
{
	$dom = new DOMDocument();
	$dom->preserveWhiteSpace = false;
	$dom->formatOutput = false;
	// '<?xml encoding="utf-8" ?\>' คือเพื่อให้มันแสดงภาษาไทย. (  https://stackoverflow.com/a/8218649/128761 )
	// LIBXML_HTML_NODEFDTD คือเพื่อให้ไม่ต้องวุ่นวายกับ doctype. ( http://php.net/manual/en/libxml.constants.php )
	$dom->loadHTML('<?xml encoding="utf-8" ?>' . $string, LIBXML_HTML_NODEFDTD);
	$table = $dom->getElementsByTagName('table')->item(0);
	if (is_object($table)) {
		$tbody = $table->getElementsByTagName($getElementName)->item(0);
		if (
			(
				// if required attribute and its value are false (not required).
				$requiredAttribute === false &&
				$requiredAttributeValue === false
			) ||
			(
				// if required attribute and its value are string, it must be validate.
				is_string($requiredAttribute) &&
				is_string($requiredAttributeValue) && 
				$tbody->hasAttribute($requiredAttribute) &&
				$tbody->getAttribute($requiredAttribute) === $requiredAttributeValue
			)
		) {
			return $tbody->ownerDocument->saveHTML($tbody);
		} else {
			// อาจเปลี่ยน throw error เป็นอย่างอื่นก็ได้ เช่น return false;
			throw new \Exception('Invalid html content (' . $getElementName . ' contain no attribute or wrong value for this attribute.).');
		}
		unset($tbody);
	} else {
		// อาจเปลี่ยน throw error เป็นอย่างอื่นก็ได้ เช่น return false;
		throw new \Exception('Invalid html content (table).');
	}
	unset($table);
}
/**
 * Make string into array format.
 * @param string $string
 * @return string|array Return array on success, return original content on failed.
 */
function makeArray($string)
{
	if (is_string($string)) {
		$array = array(
			array(
				0 => $string,
			)
		);
		return $array;
	}
	return $string;
}
echo '<h3>get html table inner content</h3>' . PHP_EOL;
echo printDebug(getHTMLTableInnerContent($str, 'tbody', 'parser-repeat', 'master_data'));
echo '<h4>make it array you want.</h4>' . PHP_EOL;
echo printDebug(makeArray(getHTMLTableInnerContent($str, 'tbody', 'parser-repeat', 'master_data')));
 
 
อันนี้มาแบบพร้อมใช้เลย น่าจะได้แล้วนะ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-09-23 12:07:20 | 
                        By :
                            mr.v | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
      		  
	
     | 
   
 
                 |