2014-11-21 31 views
0

我有一個字符串,其中包含在文本編輯器上編寫的html。從文本與php分離媒體

$string ='<img src="http://example.com/image.jpg" alt="alt text" width="1200" height="1650" class="full"> 
Lorem ipsum <a href="example.com">ergo</a> ipsum'; 

像其他時間:喜歡的東西

$string ='<iframe src="http://example.com/" width="1200" height="1650" class="full"></iframe> 
<b>Lorem ipsum <a href="example.com">ergo</a> ipsum</b>' 

不知怎的,我需要得到媒體和文本到單獨的,但我不能改變的方式獲取內容上inputed字符串。

理想情況下,我會限制所有媒體到一個單獨的領域,但我不能這樣做的原因有很多。

我想用正則表達式工作,但我不能讓它工作。

我得到的最接近是通過剝離標籤,但不擺脫文字:

echo '<div class="media">'.strip_tags($sting, '<img><iframe>').'</div><br>'; 
echo '<div class="text">'.strip_tags($string, '<p><a><b><stong>').'</div>'; 

任何見解?有可能是一個非常明確的解決方案在那裏..

回答

0

不知道,如果被媒體你的意思是src參數或變量類型的值,但這裏的正則表達式之一:

// result will be in $matches[1][0] 
preg_match_all('/src=\"(.*)\"/sU', $string, $matches); 
preg_match_all('/<(img|iframe)\s/sU', $string, $matches);