Re: Search a string between <foo></foo>
| From: | Luiz Vitor | Date: | Thu, 10 May 2001 12:02:17 +0000 |
| Subject: | Re: Search a string between <foo></foo> | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-52268@lists.php.net to get a copy of this message | ||
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" <creinig@mayn.de>
To: "Luiz Vitor" <lvdamin@terra.com.br>; <php-general@lists.php.net>
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: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>
>