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!

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.