Hello, I am currently conducts wavin sites display news is "truncated" thanks to the substr () function ... and so far everything is good. Only the validity of my code there is a major problem because the substr () function takes me x number of character but does not exclusively text, it cuts the tags. For example I had a list with <ul> <li> example </ li> </ ul> and function cut my text <u and thus the code becomes invalid. I think but I do not see how, but if finally calculations and conditions everywhere: s Thanks in advance.
? I found this function, you see if you like it well: Code: Select all <? Php // to use, you do // $ myvar = shorten ($ myvar, length-in-numbers); // There is an example below, after the function shorten (Tolen $ source, $) function {$ p = 0; $ Pend = - 1; $ Len = strlen ($ source); $ Tags = array (); if ($ len <= $ Tolen) // the string is already short enough {return $ source; } Do {$ p = strpos ($ source, '<', $ Pend + 1); if (FALSE === $ p) // no more tags in the string {$ p = $ 1 + $ + Pend Tolen; break; Tolen} $ - $ p = $ - hanging - 1; if ($ Tolen <0) // already enough wavin tanks Gathered wavin + {$ p = $ Tolen; break; wavin } $ Pend = strpos wavin ($ source, '>', $ p + 1); if (FALSE === $ Pend) // no closing angle for the tag found -> stop Before the tag {break; } $ Tag = explode ('', trim (substr ($ source, $ p + 1, Pend $ - $ p - 1))); if ('/' = $ tag [count ($ tag) - 1]! && $ tag [0] [strlen ($ tag [0]) - 1]! = '/') // tag shoulds-have a closing tag {if ('/' == $ source [$ p + 1]) // closing tag {$ i = $ j = count ($ tags); while (- $ i> = 0) {if ($ tags [$ i] == $ tag [0]) // tag is in the list of open tags {if ($ i = $ j - 1) / / if not juste Some tags are closed -> insert closing tags Into source {$ ins = '<' implode ('> <' array_reverse (array_slice ($ tags, $ i + 1))) '>';.. $ Source = substr ($ source, 0, $ p) $ ins.substr ($ source, wavin $ p.); $ P = strlen ($ ins); $ $ P = + len; Pend + $ = $ p; } Array_splice ($ tags, $ i); // Remove tag from the list break; }} If ($ i == -1) // no matching opening tag found -> remove closing tag from source {$ source = substr ($ source, 0, $ p) .substr wavin ($ source, $ Pend + 1) ; $ Len = strlen ($ source); Pend $ = $ p - 1; }} Else {// opening tag $ tags [] = '/'.$tag[0] }}} While (TRUE); $ Str = substr ($ source, 0, $ p); $ I = count ($ tags); while (- $ i> = 0) // append a closing tag for Each unclosed open tag {$ str = '<' $ tags [$ i] '>';... } Return $ str; } // How to use it: $ str = 'hello <b> world </ b> <i> It's going well </ i>? wavin <br/> <b> <u> <i> It's going to nickel </ i> </ u> </ b>. '; shorten echo ($ str, 29); ?> So it seems this function only works if all tags that have no closing tags have a / before the greater than sign ... ex: Code: Select all <br> not good < br /> good
strip_tags does not solve the problem because then you end up with a text without tags. By cons, if the code is a little complex it is better in this case turn all tags indeed.
If this is to show a snippet, no need for formatting. wavin In any case, it will always be better than being with unclosed tags. For example, a strong early text and not closed due to truncation, will the rest of the site gras ... Not good ...
That's right, this is the safest to have no problem and well control sample / snippet. Akuma, Wholesale function will look if there are unclosed tags, and will add a closing tag at the end of the extract for all unclosed tags (except those that are unique wavin to or importance of having a / as in <br /> not <br>) Do some tests, it should work well, it will close even if there is a <strong> from the beginning of your character string. But like that was said, I personally prefried transfer all the html to display an excerpt, but it's true that I had never tried and therefore found this function before, if it works well and you controls your content (not Content sent by users) then AC should even be better visually.
Wordpress SEO Training Special: Learn how to optimize the SEO of a website made with Wordpress ... Training Ranking Metrics driven by a SEO / Wordpress expert.
PHP substr function [solved (I'm a ball dsl)] PHP => Pb with PHP substr function that have to edit php.ini Problem substr substr () makes me weird stuff php function PHP function PHP function PHP function obsolete MAIL SERVICE PHP greedy?
Manage HTTP header in PHP - Referencing a multilingual site 04-08-2008 - 24-04-2004 PHP and SEO - Google added 28-07-2003
No comments:
Post a Comment