2014-12-19 63 views
-2

我剛剛開始學習PHP,我無法弄清楚如何從HTML元素中獲取文本。如何獲取PHP中的HTML元素中的文本?

這裏是我的HTML代碼(我做網頁的文本編輯器):

<div id="example-one" contenteditable="true"> 
    <style scoped> 
     #example-one { margin-bottom: 10px; } 
     [contenteditable="true"] { padding: 10px; outline: 2px dashed #CCC; } 
    [ contenteditable="true"]:hover { outline: 2px dashed #0090D2; } 
</style> 
<p>Everything contained within this div is editable in a browser that supports <code>HTML5</code>. Go on, give it a try: click it and start typing.</p> 
</div> 

你們可以請說明我會得到段落元素的文字,或者div元素?

感謝收聽! -Sam

回答

1

如果我正確理解您的問題,您正在嘗試獲取HTML元素中的文本值並在PHP中使用它。

這並沒有什麼意義,因爲PHP是服務器端語言,HTML是客戶端將會看到的東西。實際上這非常好,因爲它允許您在PHP中生成HTML,將PHP與HTML分開以獲得您想要的內容。

例子:

<form method="post" action""> 
    <input name="someInputThatsAlwaysHere" /> 
    <?php 
    //Now this is PHP, so we can have some fun, like having control statements: 
    if($condition === true) { 
    ?> 
    <input name="isOnlyHereIfConditionIsTrue" /> 
    <?php } else { ?> 
    <input name="isOnlyHereifConditionIsFalse" /> 
    <?php } ?> 
</form> 
0

這是標記向上傳遞到服務器?如果是這樣的話,那麼這樣做的例子可能有助於找到here,如果你對正則表達式感到滿意的話。否則,您可以使用字符串的方式並使用PHP API中的字符串拆分來獲取內部內容。

如果你在客戶端,你將不會使用PHP,而是使用JavaScript。

var content = $("#example-one").html(); 
0

一個方法:

<?php echo'<div id="example-one" contenteditable="true"> 
    <style scoped> 
     #example-one { margin-bottom: 10px; }  
     [contenteditable="true"] { padding: 10px; outline: 2px dashed #CCC; } 
     [ contenteditable="true"]:hover { outline: 2px dashed #0090D2; } 
    </style> 
    <p>Everything contained within this div is editable in a browser that supports<code>HTML5</code>. Go on, give it a try: click it and start typing.</p> 
    </div>'; 
?> 

另一種方法:

<?php 

$html = '<div id="example-one" contenteditable="true"> 
    <style scoped> 
     #example-one { margin-bottom: 10px; } 
     [contenteditable="true"] { padding: 10px; outline: 2px dashed #CCC; } 
     [ contenteditable="true"]:hover { outline: 2px dashed #0090D2; } 
    </style> 
    <p>Everything contained within this div is editable in a browser that supports <code>HTML5</code>. Go on, give it a try: click it and start typing.</p> 
    </div>'; 
$content = getTextBetweenTags('html', $html, 1); 
foreach($content as $item) 
{ 
    echo $item.'<br />'; 
} 

?> 
如果你使用jQuery,你可以通過做一些像訪問股利或段落標記的id
相關問題