Current location: Hot Scripts Forums » Programming Languages » PHP » How can I strip certain tags between certan points?


How can I strip certain tags between certan points?

Reply
  #1 (permalink)  
Old 07-23-04, 06:05 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Question How can I strip certain tags between certan points?

Ok, this is for a client of mine. This is the way they'd like it, and I'm so-so with regex's...

The program I wrote for them adds text to pages. With each page they are able to specify whether they want carriage returns to insert break tags or whether they want to insert break tags themselves.

Well, I have this client that wants it to be able to add tags that will allow them to turn on break tags, yet add tags that will remove any break tags in between.

Here's waht I'm talking about:
Code:
This is a paragraph. Paragraphs are as old as the hills, and they've never really changed.

This is a new line. Now I'm going to type some HTML:

<nobr>
<div align="center">
<em>This is italicized</em>.
</div>
</nobr>

Now we go back to automatic line breaks?
You see what I mean? They want each new line to add a break tag, but they'd like to have any break tags between the <nobr></nobr> tags stripped out, or not have them inserted to begin with (for exactly that purpose: html tags, etc...).

Any regex experts wanna have a go at it?
Reply With Quote
  #2 (permalink)  
Old 07-23-04, 07:51 PM
rec0n rec0n is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Earth
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
You could try writing a function and replacing tags with the function result. For example:
Code:
function nobr(...) {...}
$mytext = "[nobr]This is a paragraph. Paragraphs are as old as the hills, and they've never really changed.

This is a new line. Now I'm going to type some HTML:

<nobr>
<div align="center">
<em>This is italicized</em>.
</div>
</nobr>[/nobr]

Now we go back to automatic line breaks?";
$mytext = preg_replace("#[nobr](.*?)[/nobr]#ie", "nobr('$1');", $mytext);
Now; I dunno if you get it but there's an idea for ya.
__________________
-- my bad lol
Reply With Quote
  #3 (permalink)  
Old 07-24-04, 07:47 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Ehhh, not sure if you understand what I meant, because I don't understand what that's supposed to do.

I got it figured out anyhow, just had to crack my head open and apply myself. Thanks though:

PHP Code:

preg_replace("/(\[)(nobr)(])(\r\n)*(.*)(\[\/nobr\])/esiU""stripslashes(str_replace('<br />', '', '\\5'))"$text); 


Last edited by Keith; 07-24-04 at 08:31 AM.
Reply With Quote
  #4 (permalink)  
Old 07-24-04, 08:23 AM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Here we go. Consider the tag which disallow line break is [nobr]. Based on your problem, I think we have to do double regex check. First, to strip the line break in the tag and then to strip [nobr] tag from it.

Here is an example code:
PHP Code:



//$myText refers to the data where [nobr] tag may exist

$break '#<br(\s)?(?(1)\/)>#i';

//Variable $myText points to the text you want to change.
if(preg_match_all("/\[nobr\](.*?)\[\/nobr\]/si",$myText,$match)) {
    for(
$i=0$i<count($match[1]); $i++) {
        
$replace[] = preg_replace($break,"",$match[1][$i]);    
        
//Avoid finding the same identifier in the data
        
$match[0][$i] = preg_replace("/#/","",$match[0][$i]);
        
$matches[$i] = '#'.$match[0][$i].'#';
        print 
$replace[$i];
    }
    unset(
$match);
    
$myText preg_replace($matches,$replace,$myText);

I don't know whether PHP can call function preg_replace within another preg_replace function. If it could, the above code should be optimised.
Reply With Quote
  #5 (permalink)  
Old 07-24-04, 08:27 AM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Keith
Ehhh, not sure if you understand what I men, because I don't understand what that's supposed to do.

I got it figured out anyhow, just had to crack my head open and apply myself. Thanks though:

PHP Code:

preg_replace("/(\[)(nobr)(])(\r\n)*(.*)(\[\/nobr\])/esiU""stripslashes(str_replace('<br />', '', '\\5'))"$text); 

Oh you've found it. Anyway, str_replace wouldn't substitute <BR>, <BR /> or <br>.
Reply With Quote
  #6 (permalink)  
Old 07-24-04, 08:30 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Yep, I use the XHTML break tag anyhow (nl2br() function to be more specific). I wasn't sure either if preg_replace could be used inside preg_replace... but on your suggestion I tried it and it works like a charm for both <br> as well as <br /> tags:
PHP Code:

preg_replace("/(\[)(nobr)(])(\r\n)*(.*)(\[\/nobr\])/esiU""stripslashes(preg_replace('#<br(\s)?(?(1)\/)>#i', '', '\\5'))"$text); 

Reply With Quote
  #7 (permalink)  
Old 07-24-04, 08:40 AM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
The optimised code should be:
PHP Code:

$text preg_replace("/\[nobr\](.*?)\[\/nobr\]/esiU""stripslashes(preg_replace('/<br(\s)?(?(1)\/)>/i', '', '\\1'))"$text); 


//or

preg_replace("/(\[)(nobr)(])(\r\n)*(.*)(\[\/nobr\])/esiU""stripslashes(preg_replace('/<br(\s)?(1)\/)>/i', '', '\\5'))"$text); 
Reply With Quote
  #8 (permalink)  
Old 07-24-04, 08:44 AM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
haha... finally it's done. thank you also. now i know that i can call multiple preg_replace which can save lot of my work... thanks again..
Reply With Quote
  #9 (permalink)  
Old 07-24-04, 01:43 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
No problem, thanks for the optimization. I still have a lot to learn regarding regex's.
Reply With Quote
  #10 (permalink)  
Old 03-25-05, 01:19 AM
adoxe adoxe is offline
New Member
 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Optimization ?

a good optimization at this point would rather be to put these calls on separate lines since it doesn't accelerate nor lighten the process to put this in a single line. And what you'll get from this ? Tell me what if not a hardened way to maintain your code...

Last edited by adoxe; 03-25-05 at 01:25 AM. Reason: talkin' about optimization...
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
freeware code to strip HTML tags ? xgab Perl 5 04-19-04 07:31 PM
grabing text between tags adjamlotfi PHP 0 02-22-04 08:46 AM
removing specified html tags from text memon PHP 1 01-15-04 08:56 PM
a news scipt with a points system SPIKE326 PHP 0 08-28-03 03:25 PM
New Sorting Formula In Place relledge Announcements 0 06-21-03 02:17 PM


All times are GMT -5. The time now is 04:13 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.