PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > problem with this regex to remove img tag...
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
problem with this regex to remove img tag...

Réponse
 
LinkBack Outils de la discussion
Vieux 16/03/2008, 21h43   #1
Ryan A
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut problem with this regex to remove img tag...

Hey All,

After searching I found this regex to remove img tags from a post, but running it is giving me an error, being a total noob with regex i have no idea what the heck is wrong.... heres the whole script as its tiny:

<?php

$html = " hello ".'<img src="/articles-blog/imgs/paul-fat.jpg" alt="" width="272" height="289" align="left">'. " Paul! ";

$html = preg_replace('</?img((\s+\w+(\s*=\s*(?:".*?"|\'.*?\'|[^\'">\s]+))?)+\s*|\s*)/?>', '', $html);

echo $html;
?>

Any appreciated.

TIA,
Ryan


------
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)




__________________________________________________ __________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
  Réponse avec citation
Vieux 16/03/2008, 21h50   #2
M. Sokolewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with this regex to remove img tag...

Ryan A wrote:
> Hey All,
>
> After searching I found this regex to remove img tags from a post, but running it is giving me an error, being a total noob with regex i have no idea what the heck is wrong.... heres the whole script as its tiny:
>
> <?php
>
> $html = " hello ".'<img src="/articles-blog/imgs/paul-fat.jpg" alt="" width="272" height="289" align="left">'. " Paul! ";
>
> $html = preg_replace('</?img((\s+\w+(\s*=\s*(?:".*?"|\'.*?\'|[^\'">\s]+))?)+\s*|\s*)/?>', '', $html);
>
> echo $html;
> ?>
>
> Any appreciated.
>
> TIA,
> Ryan
>
>
> ------
> - The faulty interface lies between the chair and the keyboard.
> - Creativity is great, but plagiarism is faster!
> - Smile, everyone loves a moron. :-)
>
>
>
>
> __________________________________________________ __________________________________
> Never miss a thing. Make Yahoo your home page.
> http://www.yahoo.com/r/hs



why not a simple:
$html = preg_replace('#(<[/]?img.*>)#U', '', $html);
?

- Tul
  Réponse avec citation
Vieux 16/03/2008, 22h08   #3
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Re: problem with this regex to remove img tag...

M. Sokolewicz wrote:
> Ryan A wrote:
>> Hey All,
>>
>> After searching I found this regex to remove img tags from a post, but
>> running it is giving me an error, being a total noob with regex i have
>> no idea what the heck is wrong.... heres the whole script as its tiny:
>>
>> <?php
>>
>> $html = " hello ".'<img src="/articles-blog/imgs/paul-fat.jpg" alt=""
>> width="272" height="289" align="left">'. " Paul! ";
>>
>> $html =
>> preg_replace('</?img((\s+\w+(\s*=\s*(?:".*?"|\'.*?\'|[^\'">\s]+))?)+\s*|\s*)/?>',
>> '', $html);
>>
>> echo $html;
>> ?>
>>
>> Any appreciated.
>>
>> TIA,
>> Ryan
>>
>>
>> ------
>> - The faulty interface lies between the chair and the keyboard.
>> - Creativity is great, but plagiarism is faster!
>> - Smile, everyone loves a moron. :-)
>>
>>
>>
>>
>>
>> __________________________________________________ __________________________________
>>
>> Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs

>
>
> why not a simple:
> $html = preg_replace('#(<[/]?img.*>)#U', '', $html);
> ?


Wouldn't your example do this?

<p><img src="..." /><br /> some description</p>
^--catch from here to here ??-------------^

Would it not be better if the .* was [^>]+ instead. Wouldn't the .*
catch an > even if it was like this?

Jim

>
> - Tul
>


  Réponse avec citation
Vieux 16/03/2008, 22h21   #4
M. Sokolewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Re: problem with this regex to remove img tag...

Jim Lucas wrote:
> M. Sokolewicz wrote:
>> Ryan A wrote:
>>> Hey All,
>>>
>>> After searching I found this regex to remove img tags from a post,
>>> but running it is giving me an error, being a total noob with regex i
>>> have no idea what the heck is wrong.... heres the whole script as its
>>> tiny:
>>>
>>> <?php
>>>
>>> $html = " hello ".'<img src="/articles-blog/imgs/paul-fat.jpg" alt=""
>>> width="272" height="289" align="left">'. " Paul! ";
>>>
>>> $html =
>>> preg_replace('</?img((\s+\w+(\s*=\s*(?:".*?"|\'.*?\'|[^\'">\s]+))?)+\s*|\s*)/?>',
>>> '', $html);
>>>
>>> echo $html;
>>> ?>
>>>
>>> Any appreciated.
>>>
>>> TIA,
>>> Ryan
>>>
>>>
>>> ------
>>> - The faulty interface lies between the chair and the keyboard.
>>> - Creativity is great, but plagiarism is faster!
>>> - Smile, everyone loves a moron. :-)
>>>
>>>
>>>
>>>
>>>
>>> __________________________________________________ __________________________________
>>>
>>> Never miss a thing. Make Yahoo your home page.
>>> http://www.yahoo.com/r/hs

>>
>>
>> why not a simple:
>> $html = preg_replace('#(<[/]?img.*>)#U', '', $html);
>> ?

>
> Wouldn't your example do this?
>
> <p><img src="..." /><br /> some description</p>
> ^--catch from here to here ??-------------^
>
> Would it not be better if the .* was [^>]+ instead. Wouldn't the .*
> catch an > even if it was like this?
>
> Jim
>
>>
>> - Tul
>>

>

The U modifier prevents that, it makes the expression ungreedy. So
unless the OP has a tag with an > embedded in it, it should be fine.

- Tul
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h26.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12290 seconds with 12 queries