Welcome Guest, Not a member yet? Register   Sign In
Parsing Blog Posts for first <p> and <img> elements
#2

[eluser]JoostV[/eluser]
substr and strpos are a good way to do this. Fast & efficient.
You could chuck the code into a helper, so you can reuse it and your view code stays clean.

Code:
echo get_first_paragraph($body);

Helper code:
Code:
/**
* Return the first paragraph of a string, if it exists.
* If no paragraph exists, return the entire string.
*/
function get_first_paragraph($str) {
    if (stristr($str, '</p>')) {
        return substr($str, 0, strpos($str, '</p>') + 4);
    }
    return $str;
}


Messages In This Thread
Parsing Blog Posts for first <p> and <img> elements - by El Forum - 07-29-2011, 10:40 PM
Parsing Blog Posts for first <p> and <img> elements - by El Forum - 07-30-2011, 04:53 AM
Parsing Blog Posts for first <p> and <img> elements - by El Forum - 07-30-2011, 08:56 AM
Parsing Blog Posts for first <p> and <img> elements - by El Forum - 07-30-2011, 09:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB