PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
This code is ment to newlines and other nasty stuff between two [block] tags, but should remove the [break-blocks] tag. So I want to remove anything except [break-blocks] between [/block] and [block=...
A sample of a "raw" input would be great...
- Not quite sure here, but shouldn't you loop thru all lines?
- Is there something before and after the [block]?
їblock=10%]16-08-03ї/block]
їblock=15%]Willem II - AZ :-)ї/block]
їblock=5%]1-0ї/block]
їblock=5%]їDetails]ї/block]їbreak-blocks] <- this one is getting stripped but is essential
їblock=10%]23-08-03ї/block]
їblock=15%]PSV - Willem IIї/block]їbreak-blocks]
This my UBB like code to create divs that are floating next to each other and [break-blocks] adds a div w/ 'clear: both'. The problem is that any newlines/rubbish between [/block] and [block= messes up the layout so I need to remove that. The first few blocks are displayed well and break perfectly but the second [break-blocks] is stripped by the regexp is posted and thus it doesn't break the previous blocks...
So I have to strip everything except the '[break-blocks]' tag. Stripping everyting worked, but I don't know how to keep the [break-blocks]' tag intact....
these two will do the same thing in the example you provided, but will not behave the same in all cases. if you'd like more clarification on that (not to insult your intelligence, just not sure how much you know about regexes), i can explain more.
?>
Last edited by will on Wed Aug 20, 2003 5:02 am, edited 2 times in total.
you don't need the question mark after ".*" since it means zero or more... you will also need the U modifier to make preg_repalce "ungreedy" (someone discusses it in the user comments in the manual page for preg_replace)
//strip anything between 2 blocks
$input = preg_replace("@\[\/block\].*?(\[break-blocks\])?.*?\[block=@is","[/block]\\1[block=", $input);
Notice the @? EDIT: well, not exactly 'that', but it makes it easier anyway.
I wasn't able to remove any question marks, this didn't work. Which can be removed?
the first and third one are not needed, unless you intend for them to match a literal '?', which i doubt (if that is the case however, they need to be escaped as the the previous post explains). a question mark matches zero or one of the preceeding blocks, whether it is a single character or parenthetical set. therefore the middle qmark is needed because the [break-blocks] text may or may not be present.
an asterick (*) matches zero or more instances of the preceding block. since it allows for zero instances, the question mark is not needed. you may be thinking of a plus sign (+) which matches one or more instances... in which case you would need the qmark (although i'm still not sure if that would actually work... never tried).