Jump to content

[SOLVED] Regex to Replace <div> with FCKEditor


davecox

Recommended Posts

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!

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.