Re: Trimming Text
| From: | Al | Date: | Sun, 17 Jul 2005 14:07:47 +0000 |
| Subject: | Re: Trimming Text | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-218831@lists.php.net to get a copy of this message | ||
Al wrote:
André Medeiros wrote:Here is a pattern that will get you close $pattern= "%(<[\s\S]+?>)([\s\S]+?)(</[\s\S]+?>)%"; The first bracket will get your first tag; second bracket, your text; third, the end tag. Try this... $num= preg_match_all("$pattern", $your_string, $matches); print_r($matches); I didn't test this; but, you should see all your tags and text neatly in an array. You can then use foreach($matches[x] $key=>$value) { your code to trim, etc. $new_array[$key}= $new_string; } From the print_r() you can see what "x should be. And, you'll need to reassemble your $new_array. Finally, reconstruct your output string with another foreach() The preg_replace_callback() will do all the foreach() stuff in one line. Your argument for the callback function will be "x".Greetings. I am trying to trim some text containing HTML tags. What I want to do is to trim the text without trimming the tags or html entities like and such, wich completelly break the design. Has anyone succeded on doing such a thing? phpclasses.org won't help :( Thanks in advance. AndréConsider... Making a preg pattern to capture everything between tags and then use preg_replace_callback() process the "captured text" with the called function. It will work; but it's a bit tricky. Here is a similiar code snip to get you started... $pattern= "%<[\w-/]+>%";$text= preg_replace_callback($pattern, create_function('$matches', 'return strtolower($matches[0]);'), $text);This converts all tags to lowercase. You probably should use a regular callback function rather than creating one.