php递归函数字符串摘录

php递归函数字符串摘录,php,string,recursion,Php,String,Recursion,我正在尝试用html创建一篇博客文章的摘录。我不想摘录包括任何图像,所以我包括了一个功能,带出图像,这是正常工作 我正在使用fckeditor,它将图像包装在中。如果html是 <p><img src="/yada" /> Here is some sample text. </p> 但是…如果图像是p标记内的唯一内容,则函数返回一个空字符串,这是正确的,但不可取 我尝试创建一个递归函数来传递内容,从空p标记的末尾开始,直到找到一个包含内容的p标记,但它只是

我正在尝试用html创建一篇博客文章的摘录。我不想摘录包括任何图像,所以我包括了一个功能,带出图像,这是正常工作

我正在使用fckeditor,它将图像包装在

中。如果html是

<p><img src="/yada" /> Here is some sample text. </p>
但是…如果图像是p标记内的唯一内容,则函数返回一个空字符串,这是正确的,但不可取

我尝试创建一个递归函数来传递内容,从空p标记的末尾开始,直到找到一个包含内容的p标记,但它只是不断返回一个空字符串

function get_excerpt($content) {        

    $start = strpos($content, '<p');


    if($start !== FALSE) {   

        $p_open = strpos($content, '>', $start) + 1;
        $p_close = strpos($content, '</p>', $p_open);
        $length = $p_close - $p_open;
        $p_contents = substr($content, $p_open, $length);

        $p_contents = removeImages($p_contents);
        $contentLength = strlen($content);

        $newContent = substr($content, $p_close); 

        if($p_contents == "")
        {
            get_excerpt($newContent);
        }
        else
        {
            return $p_contents;
        }

    }
    else 
    {
        return 'Excerpt not available';
    }
}


function removeImages($string)
{
    $newString = preg_replace("/<img[^>]+\>/i", "", $string);
    return $newString;
}
函数获取摘录($content){
$start=strpos($content,,$start)+1;
$p_close=strpos($content,

,$p_open); $length=$p_close-$p_open; $p_contents=substr($content,$p_open,$length); $p_contents=移除图像($p_contents); $contentLength=strlen($content); $newContent=substr($content,$p_close); 如果($p_contents==“”) { 获取摘录($newContent); } 其他的 { 返回$p_内容; } } 其他的 { 返回“摘录不可用”; } } 函数removeImages($string) { $newString=preg\u replace(“/]+\>/i”,”,$string); 返回$newString; }
可能的重复项不是重复项。我不想使用html解析器。@callumander你应该这样做。不管解析器看起来如何,它都被称为html解析器。即使解析器或多或少是它自己的一个发送。
function get_excerpt($content) {        

    $start = strpos($content, '<p');


    if($start !== FALSE) {   

        $p_open = strpos($content, '>', $start) + 1;
        $p_close = strpos($content, '</p>', $p_open);
        $length = $p_close - $p_open;
        $p_contents = substr($content, $p_open, $length);

        $p_contents = removeImages($p_contents);
        $contentLength = strlen($content);

        $newContent = substr($content, $p_close); 

        if($p_contents == "")
        {
            get_excerpt($newContent);
        }
        else
        {
            return $p_contents;
        }

    }
    else 
    {
        return 'Excerpt not available';
    }
}


function removeImages($string)
{
    $newString = preg_replace("/<img[^>]+\>/i", "", $string);
    return $newString;
}