Re: urgent !!! Please Help preg_replace !!!!!!!!!!!!!!!!!!!!!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux