class Something
{
public static function compile(&$subject, $replace, $with) {
$placeholders = array_combine($replace, $with);
$condition = '{[a-z0-9\_\- ]+:[a-z_]+}';
$inner = '((?:(?!{/?if).)*?)';
$pattern = '#{if\s?('.$condition.')}'.$inner.'{/if}#is';
while (preg_match($pattern, $subject, $match)) {
$placeholder = $match[1];
$content = $match[2];
// if empty value remove whole line
// else show line but remove pseudo-code
$subject = preg_replace($pattern,
empty($placeholders[$placeholder]) ? '' : addcslashes($content, '$'),
$subject,
1);
}
}
}
我有一個html代碼區可以播放。 Dompdf處理其餘的將我的表單轉換爲pdf。這個班級代碼幾乎是預製的。它給了我自由使用;PHP正則表達式 - 如何選擇如果等於東西
{if {dil:value}} <div class="english">ENGLISH</div> {/if}
類似的東西在html區域。但它只是檢查單選按鈕是否爲空。但我想知道選擇了哪個選項。所以我想使用一個像這樣的代碼;
{if {dil:value}=='ENGLISH'} <div class="english">ENGLISH</div> {/if}
我用手動相等檢查轉換了空的檢查行,看它是否有效;
// if empty value remove whole line
// else show line but remove pseudo-code
$subject = preg_replace($pattern,
$placeholders[$placeholder]=='english' ? '' : addcslashes($content, '$'),
$subject,
1);
它的工作,但沒有自由當然(只有當收音機等於英文)。我對正則表達式很陌生,所以我想不出來。我試圖添加$ equal變量,所以我會多次使用我的代碼進行不同的檢查;
class Something
{
public static function compile(&$subject, $replace, $with) {
$placeholders = array_combine($replace, $with);
$condition = '{[a-z0-9\_\- ]+:[a-z_]+}';
$inner = '((?:(?!{/?if).)*?)';
$equality = '(?<=~)[^}]*(?=~)';
$pattern = '#{if\s?('.$condition.')'.$equality.'}'.$inner.'{/if}#is';
while (preg_match($pattern, $subject, $match)) {
$placeholder = $match[1];
$content = $match[2];
// if empty value remove whole line
// else show line but remove pseudo-code
$subject = preg_replace($pattern,
$placeholders[$placeholder]==$equality ? '' : addcslashes($content, '$'),
$subject,
1);
}
}
}
使用此代碼的html區域;
{if {dil:value}~'ENGLISH'~} <div class="english">ENGLISH</div> {/if}
我真的認爲這是可行的,但它並沒有:)即使格局沒有得到轉換爲PDF格式,所以我可以讀我的PDF文件下的代碼後刪除。
我可能會錯過有關$匹配的內容。包含相等變量,所以$ match可能會得到第三個數組,我猜。所以我也試圖添加這個,但那也不起作用;
while (preg_match($pattern, $subject, $match)) {
$placeholder = $match[1];
$equality = $match[2];
$content = $match[3];
那麼我的目標是把檢查圖標放在預先設計的表格圖像上的特定框區域。我處理所有帶絕對位置的文本框,除了獲取哪個單選按鈕。
謝謝你的一切幫助。