Hi I have the following piece of code:
preg_split("/<br *\/?>/i", $string)
The problem is the '?>', I thought of using escape characters? Would this affect the regular expression? This may be a bit of a stupid question but I haven't encountered this sort of problem before so any help is appreciated. Thanks.
Certain characters have to be escaped.
<?php $strLong = "this is a long <br> string <BR/>divided into a few <br/> parts and then it goes <br /> on again"; echo $strLong; echo "<hr />"; $arShorterStrings = preg_split("/<br[ ]?[\/]?>/i", $strLong); while (list($vk, $strShortString) = each($arShorterStrings)) { echo $strShortString . " | "; } ?>
The forward slash before the ? is special character and thus, you need to backslash it out:
preg_split("/<br *\\/?>/i", $string)
This is how I would do it, though, to account for HTML 4.01 <br> tags...
preg_split("/<br(\\s\\/)?>/i", $string)
Thanks for your replies but it still doesn't seem to solve the problem. Maybe I didn't explain clearly enough.
When the script is parsed, it hits that regular expression and gets to the ?> and takes that as the end of the script tag and doesn't parse anything after it.
Is it possible to escape it somehow without affecting the search pattern?
Cheers. 🙂
That is what pyro meant. The delimiters you are using is the forward slash, therefore when the regexp engine sees the second occurence of the forward slash, whatever before it will be parsed and whatever after it expects the appropriate flag. You can choose a separate (non de-facto) delimeter such as # or | but when you have these characters in your search pattern you still would have to escape it.
preg_split("|<br */?>|i", $string)
but I think you should retain the standard delimiters and escape the forward slash.