regular expression, image, name, alt, title, preg_match_all

This is a discussion on regular expression, image, name, alt, title, preg_match_all within the PHP General forums, part of the PHP Programming Forums category; I'm trying to develop a regex for matching with preg_match_all, I want to match such things like image name, ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-01-2003
Adam I Agnieszka Gasiorowski Fnord
 
Posts: n/a
Default regular expression, image, name, alt, title, preg_match_all


I'm trying to develop a regex for matching
with preg_match_all, I want to match such things
like image name, image alt text, image title in
construct like this:

....html...
<div class="class" style="style">
<img src="img=name" alt="alt" title="title" />
<span class="class" style="style">
text
</span>
</div>
....html...
The rexex as for now is:

define(
'REGEX_IMAGE_NAMES_AND_TITLES_AND_ALTS_FROM_CONTEN T',
'{
(?:<\s*img\s+src\s*=\s*(?:"|\')?\s*(?:img)?\s*=\s* ) # <img>
(?>\b\S+\b) # name
(?:title\s*=\s*(?:"|\')) # title
(?>\b\S*\b)
(?:"|\')*\s*
(?:alt\s*=\s*(?:"|\')) # alt
(?>\b\S*\b)
(?:"|\')*\s*
(?:\"|\'|>|/>|\s) # <img />
}Uix'
);

, but it does not match. How can I fix it?

--
Seks, seksić, seksolatki... news:pl.soc.seks.moderowana
http://hyperreal.info { iWanToDie } WiNoNa ) (
http://szatanowskie-ladacznice.0-700.pl foReVeR( * )
Poznaj jej zwiewne kształty... http://www.opera.com 007
Reply With Quote
  #2 (permalink)  
Old 12-01-2003
Sophie Mattoug
 
Posts: n/a
Default Re: [PHP] regular expression, image, name, alt, title, preg_match_all

Adam i Agnieszka Gasiorowski FNORD wrote:

> I'm trying to develop a regex for matching
> with preg_match_all, I want to match such things
> like image name, image alt text, image title in
> construct like this:
>
> ....html...
> <div class="class" style="style">
> <img src="img=name" alt="alt" title="title" />
> <span class="class" style="style">
> text
> </span>
> </div>
> ....html...
> The rexex as for now is:
>
>define(
> 'REGEX_IMAGE_NAMES_AND_TITLES_AND_ALTS_FROM_CONTEN T',
> '{
> (?:<\s*img\s+src\s*=\s*(?:"|\')?\s*(?:img)?\s*=\s* ) # <img>
> (?>\b\S+\b) # name
> (?:title\s*=\s*(?:"|\')) # title
> (?>\b\S*\b)
> (?:"|\')*\s*
> (?:alt\s*=\s*(?:"|\')) # alt
> (?>\b\S*\b)
> (?:"|\')*\s*
> (?:\"|\'|>|/>|\s) # <img />
> }Uix'
> );
>
> , but it does not match. How can I fix it?
>
>


It's not so easy to match an entire IMG tag, because first of all the
attributes are not always in the same order. If I were you, this is what
I would do :
ereg("<img ([^>]+)>", $your_text, $img_array);
$i = 0;
foreach ($img_array as $img) {
while (ereg("^(.+)=\"(.+)\"", "", $img, $regs))
$images[$i][$regs[1]] = $regs[2];
$i++;
}

Hope this helps,

--
Cordialement,
---------------------------
Sophie Mattoug
Développement web dynamique
sophie@mattoug.net
---------------------------
Reply With Quote
  #3 (permalink)  
Old 12-01-2003
Sophie Mattoug
 
Posts: n/a
Default Re: [PHP] regular expression, image, name, alt, title, preg_match_all

Sophie Mattoug wrote:

> Adam i Agnieszka Gasiorowski FNORD wrote:
>
>> I'm trying to develop a regex for matching
>> with preg_match_all, I want to match such things
>> like image name, image alt text, image title in
>> construct like this:
>>
>> ....html...
>> <div class="class" style="style">
>> <img src="img=name" alt="alt" title="title" />
>> <span class="class" style="style">
>> text
>> </span>
>> </div>
>> ....html...
>> The rexex as for now is:
>>
>> define(
>> 'REGEX_IMAGE_NAMES_AND_TITLES_AND_ALTS_FROM_CONTEN T',
>> '{
>> (?:<\s*img\s+src\s*=\s*(?:"|\')?\s*(?:img)?\s*=\s* ) #
>> <img>
>> (?>\b\S+\b) # name
>> (?:title\s*=\s*(?:"|\')) #
>> title
>> (?>\b\S*\b)
>> (?:"|\')*\s*
>> (?:alt\s*=\s*(?:"|\')) # alt
>> (?>\b\S*\b)
>> (?:"|\')*\s*
>> (?:\"|\'|>|/>|\s) # <img />
>> }Uix'
>> );
>>
>> , but it does not match. How can I fix it?
>>
>>

>
> It's not so easy to match an entire IMG tag, because first of all the
> attributes are not always in the same order. If I were you, this is
> what I would do :
> ereg("<img ([^>]+)>", $your_text, $img_array);
> $i = 0;
> foreach ($img_array as $img) {
> while (ereg("^(.+)=\"(.+)\"", "", $img, $regs))
> $images[$i][$regs[1]] = $regs[2];
> $i++;
> }
>
> Hope this helps,



Sorry I made a mistake. Better do this:
ereg("<img ([^>]+)>", $your_text, $img_array);
$i = 0;
foreach ($img_array as $img) {
while (ereg("^(.+)=\"(.+)\"(.+)$", "", $img, $regs)) {
$images[$i][$regs[1]] = $regs[2];
$img = $regs[3];
}
$i++;
}

--
Cordialement,
---------------------------
Sophie Mattoug
Développement web dynamique
sophie@mattoug.net
---------------------------
Reply With Quote
  #4 (permalink)  
Old 12-01-2003
Adam I Agnieszka Gasiorowski Fnord
 
Posts: n/a
Default Re: [PHP] regular expression, image, name, alt, title, preg_match_all

Sophie Mattoug wrote:

> Adam i Agnieszka Gasiorowski FNORD wrote:
>
> > I'm trying to develop a regex for matching
> > with preg_match_all, I want to match such things
> > like image name, image alt text, image title in
> > construct like this:
> >
> > ....html...
> > <div class="class" style="style">
> > <img src="img=name" alt="alt" title="title" />
> > <span class="class" style="style">
> > text
> > </span>
> > </div>
> > ....html...
> > The rexex as for now is:
> >
> >define(
> > 'REGEX_IMAGE_NAMES_AND_TITLES_AND_ALTS_FROM_CONTEN T',
> > '{
> > (?:<\s*img\s+src\s*=\s*(?:"|\')?\s*(?:img)?\s*=\s* ) # <img>
> > (?>\b\S+\b) # name
> > (?:title\s*=\s*(?:"|\')) # title
> > (?>\b\S*\b)
> > (?:"|\')*\s*
> > (?:alt\s*=\s*(?:"|\')) # alt
> > (?>\b\S*\b)
> > (?:"|\')*\s*
> > (?:\"|\'|>|/>|\s) # <img />
> > }Uix'
> > );
> >
> > , but it does not match. How can I fix it?
> >
> >

>
> It's not so easy to match an entire IMG tag, because first of all the
> attributes are not always in the same order. If I were you, this is what
> I would do :
> ereg("<img ([^>]+)>", $your_text, $img_array);
> $i = 0;
> foreach ($img_array as $img) {
> while (ereg("^(.+)=\"(.+)\"", "", $img, $regs))
> $images[$i][$regs[1]] = $regs[2];
> $i++;
> }
>
> Hope this helps,


What I
really want to get out of this regex is
1) image name
2) image alt text
3) image title text
, so only those three parentheses are of capturing
kind and the rest is marked as non-capturing...
I wonder will this work (if I change this part):
(?:
(?:title\s*=\s*(?:"|\')) # title
(?>\b\S*\b)
(?:"|\')*\s*
|
(?:alt\s*=\s*(?:"|\')) # alt
(?>\b\S*\b)
(?:"|\')*\s*
)

, notice the "or" character. I guess it should
match, as the order is not important now...
I love monster regexes ;8].

--
Seks, seksić, seksolatki... news:pl.soc.seks.moderowana
http://hyperreal.info { iWanToDie } WiNoNa ) (
http://szatanowskie-ladacznice.0-700.pl foReVeR( * )
Poznaj jej zwiewne kształty... http://www.opera.com 007
Reply With Quote
  #5 (permalink)  
Old 12-01-2003
Adam I Agnieszka Gasiorowski Fnord
 
Posts: n/a
Default Re: [PHP] regular expression, image, name, alt, title, preg_match_all

Sophie Mattoug wrote:

> Sophie Mattoug wrote:
>
> > Adam i Agnieszka Gasiorowski FNORD wrote:
> >
> >> I'm trying to develop a regex for matching
> >> with preg_match_all, I want to match such things
> >> like image name, image alt text, image title in
> >> construct like this:
> >>
> >> ....html...
> >> <div class="class" style="style">
> >> <img src="img=name" alt="alt" title="title" />
> >> <span class="class" style="style">
> >> text
> >> </span>
> >> </div>
> >> ....html...
> >> The rexex as for now is:
> >>
> >> define(
> >> 'REGEX_IMAGE_NAMES_AND_TITLES_AND_ALTS_FROM_CONTEN T',
> >> '{
> >> (?:<\s*img\s+src\s*=\s*(?:"|\')?\s*(?:img)?\s*=\s* ) #
> >> <img>
> >> (?>\b\S+\b) # name
> >> (?:title\s*=\s*(?:"|\')) #
> >> title
> >> (?>\b\S*\b)
> >> (?:"|\')*\s*
> >> (?:alt\s*=\s*(?:"|\')) # alt
> >> (?>\b\S*\b)
> >> (?:"|\')*\s*
> >> (?:\"|\'|>|/>|\s) # <img />
> >> }Uix'
> >> );
> >>
> >> , but it does not match. How can I fix it?
> >>
> >>

> >
> > It's not so easy to match an entire IMG tag, because first of all the
> > attributes are not always in the same order. If I were you, this is
> > what I would do :
> > ereg("<img ([^>]+)>", $your_text, $img_array);
> > $i = 0;
> > foreach ($img_array as $img) {
> > while (ereg("^(.+)=\"(.+)\"", "", $img, $regs))
> > $images[$i][$regs[1]] = $regs[2];
> > $i++;
> > }
> >
> > Hope this helps,

>
> Sorry I made a mistake. Better do this:
> ereg("<img ([^>]+)>", $your_text, $img_array);
> $i = 0;
> foreach ($img_array as $img) {
> while (ereg("^(.+)=\"(.+)\"(.+)$", "", $img, $regs)) {
> $images[$i][$regs[1]] = $regs[2];
> $img = $regs[3];
> }
> $i++;
> }


Ah, now I get it. I'll try it
(with preg-functions, as I prefer them).

So I'll get something like

$images = array(
[0] => array(
src => 'image1.jpg',
alt => '',
title => 'a cow'
),
[1] ...
)

...nice :8]. Thank you!

--
Seks, seksić, seksolatki... news:pl.soc.seks.moderowana
http://hyperreal.info { iWanToDie } WiNoNa ) (
http://szatanowskie-ladacznice.0-700.pl foReVeR( * )
Poznaj jej zwiewne kształty... http://www.opera.com 007
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:05 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0