Re: urgent !!! Please Help preg_replace !!!!!!!!!!!!!!!!!!!!!
| From: | Robert Cummings | Date: | Sun, 10 Feb 2008 07:35:48 +0000 |
| Subject: | Re: urgent !!! Please Help preg_replace !!!!!!!!!!!!!!!!!!!!! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-269019@lists.php.net to get a copy of this message | ||
On Sun, 2008-02-10 at 14:33 +0800, LKSunny wrote:
> //please run this script, you can get what problem i got
>
> $html = <<<eof
> <a href="aaa.html">aaa</a>
>
> <a href="bbb.html">cfdfd</a>
>
> <a href="aaa.html">sfs
> sfsrbbb
> sfds</a>
>
> <a href="aaa.html">cc
> gd
> c</a>
>
> <a href="aaa.html">ddd
> 123</a>
> eof;
>
> /*
> i want replace any |<a.has bbb.</a>|, like
> <a href="bbb.html">cfdfd</a>
>
> <a href="aaa.html">sfs
> sfsrbbb
> sfds</a>
> */
>
<?php
$reps = array
(
array
(
'match' => '#<a.*</a>#Uims',
'check' => '#bbb#',
'replace' => 'X',
),
);
foreach( $reps as $criteria )
{
if( preg_match_all( $criteria['match'], $html, $matches ) )
{
foreach( $matches[0] as $match )
{
if( preg_match( $criteria['check'], $match ) )
{
$html = str_replace( $match, $criteria['replace'], $html );
}
}
}
}
?>
This does what you want to $html, but it doesn't do it in a single
regex. I'm not sure it can be done with a single regex, and if it can, I
doubt it's simple. I don't feel like investigating too far :)
Cheers,
Rob.
--
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com
|
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'