[PHP] PHP: problem with patterns

2002-08-09 Thread Alia Mikati

Hi again,
I hope u can help me with this. I have to parse an html file and replace 
src attribute of an img. I'm using 'preg_match_all' but it seems i'm 
having problem with patterns. This is the part of code that should find 
the appropriate img src:

$preg='/img(.*?)src=[\'|]'.addcslashes($original,/).'[\'|](.*?)/si';
if (preg_match_all($preg,$arrFile,$match))
{
echo $match[1][0];  
}

where $original contains the value of src that it should find. For expl, 
$original can be: ../../MyMCMSSite/images/pic1_abeoway.gif.

But the problem is it's not catching the right img src.

Hope u can help me.
Thx a lot
Alia 



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




Re: [PHP] PHP: problem with patterns

2002-08-09 Thread Analysis Solutions

Hi Alia:

On Fri, Aug 09, 2002 at 02:50:32PM +0300, Alia Mikati wrote:

 I have to parse an html file and replace 
 src attribute of an img. I'm using 'preg_match_all'

First off, if you're doing a search and replace, use preg_replace().


 having problem with patterns. This is the part of code that should find 
 the appropriate img src:
 
 $preg='/img(.*?)src=[\'|]'.addcslashes($original,/).'[\'|](.*?)/si';

The | OR operator is not for use in [] character range blocks.


 where $original contains the value of src that it should find. For expl, 
 $original can be: ../../MyMCMSSite/images/pic1_abeoway.gif.
 
 But the problem is it's not catching the right img src.

I don't know why it's not working for you.  A slightly tweaked version of
that works for me... Do note, you weren't escaping the periods in your
addcslashes() call.


?php

$document = 'begin img size=5 
src=../../MyMCMSSite/images/pic1_abeoway.gif height=3 / end';
echo htmlspecialchars($document);

$original = '../../MyMCMSSite/images/pic1_abeoway.gif';
$original = addcslashes($original, '/.');

$preg = '/img(.*?)src=[\']' . $original . '[\'](.*?)/si';
echo 'br /';
echo htmlspecialchars($preg);

$document = preg_replace($preg, ' REPLACED ', $document);
echo 'br /';
echo htmlspecialchars($document);

?

Enjoy,

--Dan

-- 
   PHP classes that make web design easier
SQL Solution  |   Layout Solution   |  Form Solution
sqlsolution.info  | layoutsolution.info |  formsolution.info
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409

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