2017-05-04 30 views
0

所以我以s模板頁兩個領域:有沒有辦法在PHP中顯示字段的文本內容?

<?php echo get_post_field('post_content', 12345); ?> 
<?php the_field('advertisement_one', 12345); ?> 

第一場呈現爲:

<p> 
<img src="test.jpg"> 
</p> 

第二場呈現爲:

<p> 
<img src="test2.jpg"> 
</p> 

有沒有一種方式,我只需從兩個字段中的「src」中提取文本內容?

我的目標是編寫/顯示:

test.jpg 

or 

test2.jpg 

回答

1

你可以試試這個:

<?php 
$str = get_post_field('post_content', 12345); 
$str = str_replace("\n", '', $str); 
$str = str_replace('<p><img src="', '', $str); 
$str = str_replace('"></p>', '', $str); 
echo $str; 
?> 
1
$content1 = get_post_field('post_content', 12345); 
$content2 = get_field('advertisement_one', 12345); 
$src1 = preg_replace('|^.+src=["\'](.*)["\'].+$|s','$1',$conten1); 
$src2 = preg_replace('|^.+src=["\'](.*)["\'].+$|s','$1',$conten2); 
1

可以使用的preg_match檢查img標籤,並獲取其SRC值

請嘗試此碼

<?php $value = get_post_field('post_content', 12345); 
$value2 = get_field('advertisement_one', 12345); 

$src1 = preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $value, $matches); 
echo $matches[1]; 

$src2 = preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $value2, $matches); 
echo $matches[1]; 

?> 
+0

非常感謝。有一點閱讀它 – Mariton

相關問題