Thanks to all you guys who helped me.
I don't know if it's the best way, but the important is that works perfect.
It does search a string between <foo></foo> in a MySQL database and, if it
finds the string, will replace for the URL.

I'm posting it below:

function search4url($source) {
   $pattern = "/<foo>(.*)<\/foo>/U";
   preg_match_all($pattern, $source, $values);

   $conexao = mysql_connect('localhost','','');
   mysql_select_db('savepoint');


   for ($i = 0; $i < sizeof($values[0]); $i++) {
 $values[0][$i] = str_replace("<foo>", '', $values[0][$i]);
 $values[0][$i] = str_replace("</foo>", '', $values[0][$i]);

 $query = 'SELECT url FROM teste WHERE palavra = \''.$values[0][$i].'\'';

 $resultado = mysql_query($query);
 $row = mysql_fetch_array($resultado);


    if (mysql_num_rows($resultado)) {
  $replacement = '<a href="' . $row['url'] . '">\\1</a>';
         $source = preg_replace($pattern, $replacement, $source, 1);
           } else {
  $replacement = '\\1';
  $source = preg_replace($pattern, $replacement, $source, 1);
    }

   }
   mysql_close($conexao);
   echo nl2br($source);
}


Luiz Vitor

----- Original Message -----
From: "Christian Reiniger" <[EMAIL PROTECTED]>
To: "Luiz Vitor" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Wednesday, May 09, 2001 1:46 PM
Subject: Re: [PHP] Search a string between <foo></foo>


> On Wednesday 09 May 2001 16:14, Luiz Vitor wrote:
> > It's still not working.
> > I'm using the pattern ([^<foo>]*) and I'm getting the first match
> > correct, but it's not getting all the other matches.
>
> [^<foo>] means "any character except '<', 'f', 'o' and '>'"
>
> You're looking for something like this:
>
> $Text = preg_replace ('/<(foo)>(.*?)<\/\\1>/', '$2', $Text);
>
> --
> Christian Reiniger
> LGDC Webmaster (http://sunsite.dk/lgdc/)
>
> REALITY.SYS corrupted ... reboot Universe [Y,n]?
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>
>
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to