[thelist] Complicated PHP Search and Replace

Richard Harb rharb at earthling.net
Wed Nov 10 16:57:06 CST 2004


Hi,


-----Original Message-----
From: danieleastwell at onetel.net.uk
Sent: Tuesday, November 9, 2004, 5:28:02 PM
> Hi,

> This is my first time posting - I've been struggling with this
> problem all day and haven't really come that close to
> resolving it, I'm hoping you can all help.

Welcome to TheList :)



> I'm developing a rudimentary content management system that
> tags up content with non-standard tags, for example,

> <link> http://web address here <linktext> link text here </link>

> for a link, and

> <bullet>bullet items here</bullet>

> for unordered lists.

The basic concept sounds a lot like it's been done in various wiki engines.
Though the exact syntax is different (even amond the engines) the principle and
I suppose difficulties should be similar.


> I've worked out the simple str_replace for the <link> tags to
> create working hyperlinks on pseudo tags pulled from the
> database to the viewed HTML, but am struggling with the coding
> of the unordered lists.

> I have this psuedo-code so far, that operates on the content
> after having converted the <bullet> tags to conventional
> <ul><li> opening and closing tags (only at the beginning and
> end of an unordered list):

> go through $content and for every <ul>, find <li>,  then,
> until you find </li>, find \n and replace with </li>\n<li>

> and have given it this structure:

> for($i=0 ; $i == strlen($content); $i++){ // all of the content
>         for( ; ;){ // every <ul>
>             if(){ // there is a <li>
>                 for( ;;){ // until there is a </li>
>                     $content = str_replace("\n",
> "</li>\n<li>", $content);
>                 }
>             }    
>         }
>     }   


> The problem comes with the coding. The iteration over the
> content does not seem to work, and I can't get my head round
> the correct way of thinking in order to operate a string
> function on a substring, only from a particular starting
> point/needle.

> Any advice would be gratefully received.

On a similar problem someone had I pointed to the tikiwiki (sourceforge), where
the programmers did a neat job of translating custom notation to (X)HTML. This
included tables, lists, links, and a lot more ...

I know that if you decided to check out the code and try to find out what's
going on looks intimidating at first (it was for me) - but eventually I found a
lot of good concepts in there.

There are plenty other solutions out there - phpwiki might be one. It certainly
is leaner, so the relevant code might be easier to find - to see how it was
solved. I've used it before, but didn't scrutinize the code .. so I can't tell
how well done the solution in there might be.

Hope this helps

Richard




More information about the thelist mailing list