Forum Moderators: coopster

Message Too Old, No Replies

converting to textile format

Which PHP functions to use

         

ianevans

8:23 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



I'm thinking of converting my homebrewed CMS to allow me to enter news articles in Textile format. Much faster entries and I can let the CMS handle the xhtml formatting after I submit.

Of course, this means going through previously submitted articles and changing the formatting to the new style. Most of the stuff will be simple search and replace, but URLs are going to be a whole different ball o' wax. With hundreds of articles, an automated approach would leave some of my hair intact.

E.g. an URL in Textile formatting is represented as "linktext":url

This is where we get into my weak area...regex.

My initial thought would be to explode the story with </a>. That way, each story segment in the array would end with the linktext being any text after the >

Where I'm stuck is working out how to get the URL out of the href part and then tossing it back into the string as textile format.

After exploding with </a>, a string segment would look like this:

Millions of filmgoers went to see the new movie about
<a href="http://www.example.com">Example

Which needs to be replaced with:

Millions of filmgoers went to see the new movie about
"Example":http://www.example.com

Any ideas?

hiker_jjw

8:26 pm on Sep 18, 2004 (gmt 0)



Regular Expressions are the key.

ianevans

8:57 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



I realize that. :-)

But as I said in the message, "This is where we get into my weak area...regex."

hafnius

1:12 am on Sep 19, 2004 (gmt 0)

10+ Year Member



Is it not an option just to leave your old links as is?

As far as i know textilephp will leave your old links alone and they will work fine without you having to change them into Textile Markup.

/Hafnius

[edited by: ergophobe at 1:24 am (utc) on Sep. 19, 2004]
[edit reason] URL deleted as per TOS [/edit]

mincklerstraat

8:06 am on Sep 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$newhtml =
preg_replace("#<a[\s]+href="([^"]*)">([^<]*)</a>#i", "$1":$2, $yourhtml);

with the disclaimer: it often occurs that I have to check out regex resouces when I debug my regexes, and I haven't checked this one, so see if it works, and if no one posts corrections to this formula, [be2.php.net...] is the resource I use most as a regex cribsheet, lots of nice info all on one page.

Note: this won't catch any links with a "target" or set as an id or class - but that wasn't requested, and I don't know how Textile handles such things.