poddys Posted February 1, 2013 Share Posted February 1, 2013 I am coding a series of preg_replace expressions to try and strip unnecessary html/css code from text that was originally created using FCKEditor and is being copied programatically into CKEditor. All works well until I try to do a search/replace on a string that ends with a ">". No matter what I try this does not work, it either appears to do nothing or it errors and my resulting text is empty. In this case, if a table definition does not have a width defined, I want to add a width of 100%. The old/new strings and replace expression (str_replace in this case) are below. $oldstring = 'cellspacing="0">'; $newstring = 'cellspacing="0" width="100%">'; $textstring = str_replace($oldstring, $newstring, $textstring); It seems that the problem is related to the ">". If I take this out, it works ok. However I want to be sure that "cellspacing" is the last tag for a table definition (the tags were added automatically by FCKEditor and are always in the same order). Thanks for any help. I have been tearing my hair out for hours over this. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 1, 2013 Share Posted February 1, 2013 Is the > stored as its html entity - > It would display as a > in a browser and as > in the 'view source' of the output, and you would need to search for and replace it using - > Quote Link to comment Share on other sites More sharing options...
poddys Posted February 1, 2013 Author Share Posted February 1, 2013 Good thinking, but it didn't seem to work unfortunately. If I view the html text in Source mode using CKEditor it shows me ">" rather than ">" so I do believe this is not encoded. I changed my code to the following: $oldstring = 'cellspacing="0">'; $newstring = 'cellspacing="0" width="100%">'; $textstring = str_replace($oldstring, $newstring, $textstring); but no joy. The code is all on one line too, so no newline characters etc. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 1, 2013 Share Posted February 1, 2013 Is there any white-space (tab, space) after the " and before the > ? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 1, 2013 Share Posted February 1, 2013 Why don't you post your *actual* code and input, rather than trying to simulate it. Quote Link to comment Share on other sites More sharing options...
poddys Posted February 1, 2013 Author Share Posted February 1, 2013 BINGO! Thanks PFM, I guess it was staring me in the face all the time, there was a space before the ">". I changed this to use the following and it now works: $textstring = preg_replace('/cellspacing="0"[ ]*>/', 'cellspacing="0" width="100%"\>', $textstring); I was under the impression that the problem was the diamond, so leading myself on a false trail. Appreciate the speedy and rapid help. Tony Quote Link to comment 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.