[英]How to get all text between two tags including php tags
我有這個字符串:
$str = 'This is the content total <div class="brown">This content brown</div> and this another content <div class="red">This is content brown</div> and this finish.';
我想在 $str 中找到'<div class="brown">'
和</div>
之間的所有內容,但我也想要標簽。
我想在一個 var $find = '<div class="brown">This content brown</div>';
中獲得這個
然后刪除獲取的部分初始文本
$str = 'This is the content total and this another content <div class="red">This is content brown</div> and this finish.';
我試過這個 function:
function get_string_between($string, $start, $end)
{
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
但是這個function沒有獲取標簽,我也想要標簽。
謝謝。
我已經重寫了您的get_string_betwen
方法。 並包括我用來測試它的代碼。 這符合您的要求嗎? 要獲取另一個字符串,只需使用如下所示的str_replace
。
$str = 'This is the content total <div class="brown">This content brown</div> and this another content <div class="red">This is content brown</div> and this finish.';
$result = get_string_between($str, "<div", "</div>");
echo "<pre>" . htmlentities($result) . "</pre>";
$cleanedString = str_replace($result, "", $str);
echo "<pre>" . htmlentities($cleanedString) . "</pre>";
function get_string_between($string, $start, $end) {
$offset = strpos($string, $start);
$len = strpos($string, $end) - $offset + strlen($end);
return substr($string, $offset, $len);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.