Old browser strip function

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Old browser strip function

Post by Shendemiar »

Im trying to alter a wabpage to pure simple text+html for old browsers.

// This array is for stripping opening and closing tags AND what's in between

Code: Select all

$tags_and_content_to_strip = Array("title");

foreach ($tags_and_content_to_strip as $tag) {
       $contents = preg_replace("/<" . $tag . ">(.|\s)*?<\/" . $tag . ">/","",$contents);
}
I want to alter it to remove tags like span and div etc, that are of form <DIV something...>

So, i take the ">" of the expression, and add one " ", tried "\ " too, but no luck.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

when correctly implemented, an unknown tag is simply ignored.. last I checked..
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Thanks, i allready got it:

Code: Select all

// poista <DIV...> JA </DIV>
    $a = preg_replace("/<(D|d)(I|i)(V|v).(.|\s)*?>/","",$a);
    $a = preg_replace("/<\/(D|d)(I|i)(V|v)>/","",$a);

  // poista <SPAN...> xxx </SPAN>
    $a = preg_replace("/<(S|s)(P|p)(A|a)(.|\s)*?<\/(S|s)(P|p)(A|a)(N|n)>/","",$a);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your span code will destroy the data it contains and will not find spans across multiple lines.
Your div code will have same for the latter issue if the tag itself spans across lines.
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

feyd wrote:Your span code will destroy the data it contains and will not find spans across multiple lines.
Your div code will have same for the latter issue if the tag itself spans across lines.
Span needs to clear all thats between the span. I'm rather proud of myself! How do i add the multiline support to it?

(Code i use this for, never have tag split in two lines, the stuff between yes, but no tag)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use the s pattern modifier and add entire tag matching so that it'll handle across lines for the tags themselves.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Though I don't believe the regex does what you want...

/<(S|s)(P|p)(A|a)(.|\s)*?<\/(S|s)(P|p)(A|a)(N|n)>/
should simply have an i modifier.
/<(s)(p)(a)(.|\s)*?<\/(s)(p)(a)(n)>/i

;)
Post Reply