2016-09-07 87 views
3

Iam截斷php中的大字段。它的所有工作正常,除了iam在下面的行上發生錯誤。 Iam用超鏈接截斷超過25個字符。當我點擊更多鏈接時,JavaScript警報會觸發實際數據。php截斷字段問題

$length_limit = 25; //limit to this number of characters 
$actual_length = strlen($value); //count characters in the $value 
$original_text = $value; 
$truncated_text = substr($value,0,$length_limit); 


if($actual_length <= $length_limit){ 
$value = $original_text; 
} else { 
$value = $truncated_text." ... <a onclick='alert(\"'.$original_text.'\")'>more</a>"; 

Iam從最後一行收到錯誤$ value = ....可能是一些quoatation標記問題。有人可以幫助我一樣。

+0

你得到了什麼錯誤? –

+0

Iam收到錯誤「SyntaxError:未終止的字符串文字」 –

+0

使用'stripslashes'功能 –

回答

3

試試這個

echo $value = $truncated_text." ... <a onclick=\"alert('".$original_text."')\">more</a>"; 
+0

完美。它的工作正常。非常感謝你。 –

+0

歡迎您將其標記爲正確,如果它對我來說還行 –

+0

+10因爲它是正確的。:) –

2

你可以像下面(他們中的任何一個): -

echo $value = $truncated_text.' ... <a onclick=\'alert("'.$original_text.'")\'>more</a>'; 

或者

echo $value = $truncated_text." ... <a onclick=\"alert('".$original_text."')\">more</a>"; 

彈出窗口代碼: -

<style> 
#edit_price_background_overlay { 
    background: rgba(0, 0, 0, 1) none repeat scroll 0 0; 
    bottom: 0; 
    display: none; 
    overflow-y: auto; 
    position: fixed; 
    top: 0; 
    width: 100%; 
    z-index: 999999; 
} 
#mainnew_window { 
    color: white; 
    float: left; 
    margin: 20px; 
    padding: 100px; 
    text-align: center; 
} 
</style> 
<div id="edit_price_background_overlay"> 
    <div id="mainnew_window"> 

    </div> 
</div> 
<?php 
$value = 'dhgffdhgfhfhfhghgfhgfhgfhfghfghgfhgfhgfhgfgfhgfhgfhgfhgfhfgrtdyretrertertretgfdvfgvdfgdfbdfgdfbgfnbgbgfhnhhethfgbgfdnggrehgteggbfdvgfdfgergfdgfdrfgrdfgert4gtrhnfgbfdbvcvcbvcbbvcbhrgdghgyfgbfdbgfvfdbtgf'; 
$length_limit = 25; //limit to this number of characters 
$actual_length = strlen($value); //count characters in the $value 
$original_text = $value; 
$truncated_text = substr($value,0,$length_limit); 


if($actual_length <= $length_limit){ 
echo $value = $original_text; 
} else { 
echo $value = $truncated_text." ... <a onclick=\"showdata('".$original_text."','mainnew_window','edit_price_background_overlay')\">more</a>"; 
} 
?> 

<script> 
function showdata(mytext,innerdiv,outerdiv){ 

    var elem = document.getElementById(innerdiv); 
    var elem2 = document.getElementById(outerdiv); 
    console.log(elem); 
    if(typeof elem !== 'undefined' && elem !== null) { 
     document.getElementById(innerdiv).innerHTML = mytext; 

     document.getElementById(outerdiv).style.display = 'block'; 
     } 
} 

</script> 

注: - 把這個整個代碼php文件,因爲它是和檢查。

+0

謝謝你阿南德。這個解決方案也適用於我。 :) –

+0

@SanjuMenon我也添加了彈出窗口代碼。檢查一次。只是爲了您的幫助。:) :) –

+0

非常感謝你Anant。當然,我會檢查它,讓你知道這一點。再一次非常感謝你的幫助。 :) –