You are here: Re: [PHP] Re: Trimming Text « PHP « IT news, forums, messages
Re: [PHP] Re: Trimming Text

Posted by Andrι Medeiros on 07/17/05 19:00

I've succeded on doing this task :)

I had to make a few assumptions (like HTML is formatted correctly),
but, other than that, it worked just fine.

I'll post the code on monday.

Thanks for all your help guys. You've been GRRRREAT (you know... like
Tony says ;) )

On 7/17/05, Al <news@ridersite.org> wrote:
> Al wrote:
> > AndrΓ© Medeiros wrote:
> >
> >> 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 &nbsp;
> >> 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.
>
>
> 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".
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация