Re: PHP: problem with patterns
| From: | Analysis & Solutions | Date: | Fri, 09 Aug 2002 17:02:53 +0000 |
| Subject: | Re: PHP: problem with patterns | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-111846@lists.php.net to get a copy of this message | ||
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