davecox Posted June 2, 2008 Share Posted June 2, 2008 Hi, I'm creating a simple CMS system - I have some test data, which I am reading from the database: <div id='leftCol' class='editable'>My Content</div> I have written some regex to match this, and extract the ID and the Contents, so I can then create an instance of the FCKEditor with the correct values. Here is my regex: /<div[A-Za-z0-9'= ]*id='([\S]+)'[A-Za-z0-9=' ]*class='editable'>([\s\S]*)<\/div>/ I have tested this with various online regex checkers - which give me the exact results I am expecting. The expected data returned is: Array ( [0] => <div id='leftCol' class='editable'>My Content</div> [1] => leftCol [2] => My Content ) Great, right? Nope. When I put this all in to PHP it doesn't find any matches. I have tried preg_match and preg_replace. My PHP code is below: $rxControls="/<div[A-Za-z0-9'= ]*id='([\S]+)'[A-Za-z0-9=' ]*class='editable'>([\s\S]*)<\/div>/"; $bodyText=$row['ContentPartContent']; // <div id='leftCol' class='editable'>My Content</div> echo(preg_match($rxControls,$bodyText,$aMatches)); // echos '0' echo($aMatches[0]); // is blank Thanks for any help you can offer, its confusing me! Link to comment https://forums.phpfreaks.com/topic/108343-solved-regex-to-replace-ltdivgt-with-fckeditor/ Share on other sites More sharing options...
davecox Posted June 2, 2008 Author Share Posted June 2, 2008 Solved, as I am a retard. I hadn't told to regex to allow the Class and ID attributed in any order. Now I have, and it works. Link to comment https://forums.phpfreaks.com/topic/108343-solved-regex-to-replace-ltdivgt-with-fckeditor/#findComment-555522 Share on other sites More sharing options...
hansford Posted June 2, 2008 Share Posted June 2, 2008 your pattern is fine. The problem might lie in $row['ContentPartContent']; check out the results of my test. -------------- $search = "<html><body><div id='leftCol' class='editable'>My Content</div></body></html>"; $pattern = "/<div[A-Za-z0-9'= ]*id='([\S]+)'[A-Za-z0-9=' ]*class='editable'>([\s\S]*)<\/div>/"; $replace = "yadiyadi"; $str = preg_replace($pattern,$replace, $search); preg_match($pattern,$search,$matches); echo $str; //results = yadiyadi echo $matches[0]; //results = My Content Link to comment https://forums.phpfreaks.com/topic/108343-solved-regex-to-replace-ltdivgt-with-fckeditor/#findComment-555523 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.