PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > a little with preg_match
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
a little with preg_match

Réponse
 
LinkBack Outils de la discussion
Vieux 26/04/2008, 17h38   #1
nintesa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut a little with preg_match

Hello... I'm trying to write a regular expression to match the content
of a html tag.

I need to match the content of <h1> i.e.

<h1>Hello World</h1>
<h1 class="red_background">Hello World</h1>
<h1><img src="red.gif"/></h1>

etc...

can anyone me?

Thanks!!
  Réponse avec citation
Vieux 26/04/2008, 17h42   #2
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match

..oO(nintesa)

>Hello... I'm trying to write a regular expression to match the content
>of a html tag.
>
>I need to match the content of <h1> i.e.
>
><h1>Hello World</h1>
><h1 class="red_background">Hello World</h1>
><h1><img src="red.gif"/></h1>
>
>etc...
>
>can anyone me?


Try this pattern:

#<h1[^>]*>(.+?)</h1>#

Micha
  Réponse avec citation
Vieux 26/04/2008, 17h45   #3
nintesa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match

Michael Fesser ha scritto:
> .oO(nintesa)
>
>> Hello... I'm trying to write a regular expression to match the content
>> of a html tag.
>>
>> I need to match the content of <h1> i.e.
>>
>> <h1>Hello World</h1>
>> <h1 class="red_background">Hello World</h1>
>> <h1><img src="red.gif"/></h1>
>>
>> etc...
>>
>> can anyone me?

>
> Try this pattern:
>
> #<h1[^>]*>(.+?)</h1>#
>
> Micha


It's working!! thanks!
  Réponse avec citation
Vieux 26/04/2008, 17h52   #4
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match

nintesa wrote:
> Hello... I'm trying to write a regular expression to match the content
> of a html tag.
>
> I need to match the content of <h1> i.e.
>
> <h1>Hello World</h1>
> <h1 class="red_background">Hello World</h1>
> <h1><img src="red.gif"/></h1>
>
> etc...
>
> can anyone me?
>
> Thanks!!


preg_match('|<h1.*?>(.*?)</h1>|',$b,$matches);


  Réponse avec citation
Vieux 26/04/2008, 18h09   #5
nintesa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match

Paul Lautman ha scritto:
> nintesa wrote:
>> Hello... I'm trying to write a regular expression to match the content
>> of a html tag.
>>
>> I need to match the content of <h1> i.e.
>>
>> <h1>Hello World</h1>
>> <h1 class="red_background">Hello World</h1>
>> <h1><img src="red.gif"/></h1>
>>
>> etc...
>>
>> can anyone me?
>>
>> Thanks!!

>
> preg_match('|<h1.*?>(.*?)</h1>|',$b,$matches);
>
>


Are all working... but I can't make it work with tag <a>...
  Réponse avec citation
Vieux 26/04/2008, 18h12   #6
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match

..oO(nintesa)

>Are all working... but I can't make it work with tag <a>...


Same thing, if you just want the content of it. Or what's the problem
now? Some more details, please.

Micha
  Réponse avec citation
Vieux 26/04/2008, 18h15   #7
nintesa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match

Michael Fesser ha scritto:
> .oO(nintesa)
>
>> Are all working... but I can't make it work with tag <a>...

>
> Same thing, if you just want the content of it. Or what's the problem
> now? Some more details, please.
>
> Micha


ok...

I have a page content in with I have some links like...

<a href="mypage.php" class="test" alt="good" title="good title">Boing</a>

I'm trying to get out:

mypage.php (and similar)
test (and similar)
good
good title

Boing

etc...

Thanks for your !
  Réponse avec citation
Vieux 26/04/2008, 18h34   #8
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match

..oO(nintesa)

>I have a page content in with I have some links like...
>
><a href="mypage.php" class="test" alt="good" title="good title">Boing</a>
>
>I'm trying to get out:
>
>mypage.php (and similar)
>test (and similar)
>good
>good title
>
>Boing
>
>etc...


That's a bit beyond the scope of regular expressions. It would be easier
to use an HTML parser to turn the page into a DOM tree, where you can
use XPath to access any arbitrary node (elements, attributes, values).
Have a look at the DOM extension, especially DOMDocument->loadHTML() to
begin with. See the manual for details and examples.

Micha
  Réponse avec citation
Vieux 26/04/2008, 19h07   #9
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match

nintesa wrote:
> Michael Fesser ha scritto:
>> .oO(nintesa)
>>
>>> Are all working... but I can't make it work with tag <a>...

>>
>> Same thing, if you just want the content of it. Or what's the problem
>> now? Some more details, please.
>>
>> Micha

>
> ok...
>
> I have a page content in with I have some links like...
>
> <a href="mypage.php" class="test" alt="good" title="good
> title">Boing</a>
> I'm trying to get out:
>
> mypage.php (and similar)
> test (and similar)
> good
> good title
>
> Boing
>
> etc...
>
> Thanks for your !


Someone did something similar in this thread:
http://groups.google.co.uk/group/com...1a97c30642065d


  Réponse avec citation
Vieux 26/04/2008, 22h00   #10
nintesa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match


> Someone did something similar in this thread:
> http://groups.google.co.uk/group/com...1a97c30642065d
>
>


This seems to work:

  Réponse avec citation
Vieux 26/04/2008, 22h01   #11
nintesa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match

nintesa ha scritto:
>
>> Someone did something similar in this thread:
>> http://groups.google.co.uk/group/com...1a97c30642065d
>>
>>

>
> This seems to work:
>



function tag_param_content($content,$tag,$param) {
if ($tag!='img') {
preg_match_all('|<'.$tag.'.*?.$param.'=["\'](.*?)["\'].*?>.*?</'.$tag.'>|',$content,$match);
} else {
preg_match_all('|<'.$tag.'.*?'.$param.'=["\'](.*?)["\'].*?>|',$content,$match);
}
return $match[1];
}
  Réponse avec citation
Vieux 01/05/2008, 15h04   #12
leing.sky@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a little with preg_match

On Apr 27, 12:38am, nintesa <nint...@nomaaaaail.it> wrote:
> Hello... I'm trying to write a regular expression to match the content
> of a html tag.
>
> I need to match the content of <h1> i.e.
>
> <h1>Hello World</h1>
> <h1 class="red_background">Hello World</h1>
> <h1><img src="red.gif"/></h1>
>
> etc...
>
> can anyone me?
>
> Thanks!!


$pattern = "/<h1.*?>(.*)<\/h1>/";
test it.
  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 18h37.


É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,16798 seconds with 20 queries