lfernando Posted April 26, 2012 Share Posted April 26, 2012 Hi there, I have a string with a bunch of line breaks tags (<br/>) that have stuff inside, for example: Hello<br <del>/>This is</del> <ins>How are you?<br </ins>/> I basically need to find <br XXX/> and replace it with <br/> XXX so my code should look like this Hello<br/> <del>This is</del> <ins>How are you?</ins><br/> I tried using a find and replace array but I know this would be so much easier with regex! Little help!? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/261635-replace-with-something/ Share on other sites More sharing options...
ManiacDan Posted April 26, 2012 Share Posted April 26, 2012 How did you end up with such invalid code? Wouldn't it be easier to just...fix whatever is generating this code? It won't even display right in a browser. However: echo preg_replace('#(<br)(.+?)/>#', "\\1 />\\2", $a); Quote Link to comment https://forums.phpfreaks.com/topic/261635-replace-with-something/#findComment-1340681 Share on other sites More sharing options...
abareplace Posted April 26, 2012 Share Posted April 26, 2012 Use two replacements to put </ins> before </br> <?php $a = 'Hello<br <del>/>This is</del> <ins>How are you?<br </ins>/>'; $a = preg_replace( '|<br (<[a-z]+>)/>|', '<br>\\1', $a ); $a = preg_replace( '|<br (</[a-z]+>)/>|', '\\1<br>', $a ); echo $a; ?> Quote Link to comment https://forums.phpfreaks.com/topic/261635-replace-with-something/#findComment-1340686 Share on other sites More sharing options...
lfernando Posted April 26, 2012 Author Share Posted April 26, 2012 Thank you! Yes I know the code is messy.... My users update documents using a WYSIWYG editor (www.tinymce.com). Whenever a change is made, it's recorded in the history. For this I use Paul Butler's string diff algorithm (http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/) For example, the document says "Hello are you there?" and the user changes it to "Hello who is there?". The history will say: "2012.10.40: Hello are you who is there?" For some reason whenever the string has a path to a file, or bullet points, or in some other scenarios, some strikethrough tag (<del>) and/or underline tag (<ins>) gets placed inside the break. I think it may be the combination of the algorithm and the WYSIWYG editor. I looked at them trying to figure out what whas happening but couldnt, figured it'd be easier just to "clean up" the history before publishing instead of investing any more time in these two pieces of code that otherwise work well for me. Thanks again for your code I will try it out ! Quote Link to comment https://forums.phpfreaks.com/topic/261635-replace-with-something/#findComment-1340687 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.