[thelist] Complicated PHP Search and Replace

Chris spam at cimmanon.org
Tue Nov 9 16:52:26 CST 2004

On Tue, 9 Nov 2004 16:28:02 GMT, <danieleastwell at onetel.net.uk> wrote:

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

Tip:  try printing debugging code in your loops (such as print $i . '<br  
/>') to determine how often a loop is being performed and which areas are  
actually being processed.  It makes it a lot easier to find where the  
problem is located.

I don't think your way of doing it is particularly efficient, but I don't  
have the time to think up a better method (a solid preg_match would be a  
good choice if you take advantage of the pattern matching, but you're on  
your own because I'm a novice at regular expressions).  So here's a few  
things I see to at least improve your existing code.

Your for loop says "while $i is equal to strlen($content), keep performing  
this set of actions, then increment $i".  The problem is that $i is never  
equal to strlen($content), so it never goes through the loop and never  
gets incremented.  Try $i <= strlen($content).

The other thing I see is that you're only looking for the newline  
character (\n).  When it comes to forms, older Macs will submit a carriage  
return (\r), Unix and I think Mac OS X+ will use (\n), and Windows uses  
both (\r\n).  You don't indicate whether or not this is the raw data  
coming from a form.  If this is the case, try str_replace(array("\n",  
"\r", "\r\n"), "</li>\n<li>", $content);

Just so you know, it's never a good idea to call a function like that in  
the for loop because it calls the function every time it loops.  You'll  
get better performance out of $len = strlen($content); for ($i = 0; $i <=  
$len; $i++) { [...] }


More information about the thelist mailing list