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 > Regex to catch <p>s
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Regex to catch <p>s

Réponse
 
LinkBack Outils de la discussion
Vieux 06/05/2008, 02h59   #1
Ryan S
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Regex to catch <p>s


Hey all!

To say I suck at regex is an understatement so really need any I can get on this, I have a page of text with different html tags in them, but each "block" of text has a <p> or a < class="something"> tag... anybody have any regex that will catch each of these paragraphs and put then into an array
example:
array[0]="<p> first block </p>";
array[1]="<p class="blah"> block X</p>";

Thanks!
R




__________________________________________________ __________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ
  Réponse avec citation
Vieux 06/05/2008, 03h10   #2
Eric Butera
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Regex to catch <p>s

On Mon, May 5, 2008 at 9:59 PM, Ryan S <genphp@yahoo.com> wrote:
> To say I suck at regex is an understatement so really need any I can get on this, I have a page of text with different html tags in them, but each "block" of text has a <p> or a < class="something"> tag... anybody have any regex that will catch each of these paragraphs and put then into an array



If you're using php5 you can use DOM's getElementsByTagName.

If you still think you need to do some sort of regex it is possible
but it will be buggy at best.
  Réponse avec citation
Vieux 06/05/2008, 08h48   #3
Aschwin Wesselius
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Regex to catch <p>s

Ryan S wrote:
> Hey all!
>
> To say I suck at regex is an understatement so really need any I can get on this, I have a page of text with different html tags in them, but each "block" of text has a <p> or a < class="something"> tag... anybody have any regex that will catch each of these paragraphs and put then into an array
> example:
> array[0]="<p> first block </p>";
> array[1]="<p class="blah"> block X</p>";
>
> Thanks!
> R
>

Hi,

Maybe the example is overkill, but I give you a quick setup that can
save you some time finding HTML tags with a certain attribute.

<?php

$html = <<<END_OF_HTML

<b>hello</b>
<b class="blah">hello</b>
<p>hello</p>
<p class="blah">hello</p>
<a>hello</a>
<a href="url">hello</a>
END_OF_HTML;

$tags = array();
$tags[] = 'p';
$tags[] = 'a';

$tags = implode('|', $tags);

$pattern = '/<('.$tags.')[^>]*>/i';

echo $pattern."\n";

preg_match_all($pattern, $html, $matches);

var_dump($matches);

?>

I'm not an expression guru either, but I think it works OK. I had to
find 'link', 'img', 'a' and other tags in HTML and used a more complex
expression for it which worked like a charm.

It's just an example. For you, you have to leave away the 'a' tag in the
$tags array, to get what you want.

Hope it s!
--

Aschwin Wesselius

/'What you would like to be done to you, do that to the other....'/

  Réponse avec citation
Vieux 06/05/2008, 09h05   #4
Aschwin Wesselius
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Regex to catch <p>s

Aschwin Wesselius wrote:
> Ryan S wrote:
>> Hey all!
>>
>> To say I suck at regex is an understatement so really need any I
>> can get on this, I have a page of text with different html tags in
>> them, but each "block" of text has a <p> or a < class="something">
>> tag... anybody have any regex that will catch each of these
>> paragraphs and put then into an array
>> example:
>> array[0]="<p> first block </p>";
>> array[1]="<p class="blah"> block X</p>";
>>
>> Thanks!
>> R
>>

> Hi,
>
> Maybe the example is overkill, but I give you a quick setup that can
> save you some time finding HTML tags with a certain attribute.


Hi,

I'm sorry. I didn't read your request properly. Below you'll have a
correct solution:

<?php

$html = <<<END_OF_HTML

<b>hello</b>
<b class="blah">hello</b>
<p>hello</p>
<p class="blah">hello</p>
<a>hello</a>
<a href="url">this</a>
<a>hello</a>
<a href="regex yo">hello</a>
<a>hello</a>
<a id="2" href="regex yo">hello</a>
<p>that</p>
<p class="blah" title="whatever">hello</p>
END_OF_HTML;

$tags = array();
$tags[] = 'p';
$tags[] = 'a';

$attr = array();
$attr[] = 'class';
$attr[] = 'href';

$vals = array();
$vals[] = 'blah';
$vals[] = 'url';
$vals[] = 'yo';

$text = array();
$text[] = 'hello';
$text[] = 'this';
$text[] = 'that';

$tags = implode('|', $tags);
$attr = implode('|', $attr);
$vals = implode('|', $vals);
$text = implode('|', $text);

$pattern =
'/<('.$tags.')[^>]*('.$attr.')[^>]*('.$vals.')[^>]*>('.$text.')[^<\/]*<\/\1>/i';

echo $pattern."\n";
echo "--------------------\n";

preg_match_all($pattern, $html, $matches);

var_dump($matches);

?>

  Réponse avec citation
Vieux 06/05/2008, 09h30   #5
Aschwin Wesselius
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Regex to catch <p>s

Aschwin Wesselius wrote:
> Aschwin Wesselius wrote:
>> Ryan S wrote:
>>> Hey all!
>>>
>>> To say I suck at regex is an understatement so really need any
>>> I can get on this, I have a page of text with different html tags in
>>> them, but each "block" of text has a <p> or a < class="something">
>>> tag... anybody have any regex that will catch each of these
>>> paragraphs and put then into an array
>>> example:
>>> array[0]="<p> first block </p>";
>>> array[1]="<p class="blah"> block X</p>";
>>>
>>> Thanks!
>>> R
>>>

>> Hi,
>>
>> Maybe the example is overkill, but I give you a quick setup that can
>> save you some time finding HTML tags with a certain attribute.

>
> Hi,
>
> I'm sorry. I didn't read your request properly. Below you'll have a
> correct solution:

Hi,

It is obvious I haven't had my caffeine yet. This is my last try to get
the pattern straight:

<?php

$html = <<<END_OF_HTML

<b>hello</b>
<b class="blah">hello</b>
<p>those</p>
<p class="blah">hello</p>
<a>hello</a>
<a href="url">this</a>
<a>rose</a>
<a href="regex yo">hello</a>
<a>nose</a>
<a id="2" href="regex yo">hello</a>
<p>that</p>
<p class="blah" title="whatever">hello</p>
END_OF_HTML;

$tags = array();
$tags[] = 'p';
$tags[] = 'a';

$attr = array();
$attr[] = 'class';
$attr[] = 'href';

$vals = array();
$vals[] = 'blah';
$vals[] = 'url';
$vals[] = 'yo';

$text = array();
$text[] = 'hello';
$text[] = 'this';
$text[] = 'that';

$tags = implode('|', $tags);
$attr = implode('|', $attr);
$vals = implode('|', $vals);
$text = implode('|', $text);

$pattern =
'/<('.$tags.')[^>]*('.$attr.')?[^>]*('.$vals.')?[^>]*>('.$text.')[^<\/]*<\/\1>/i';

echo $pattern."\n";
echo "--------------------\n";

preg_match_all($pattern, $html, $matches);

var_dump($matches);

?>
--

Aschwin Wesselius

/'What you would like to be done to you, do that to the other....'/

  Réponse avec citation
Vieux 06/05/2008, 16h48   #6
sean.thorne@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regex to catch <p>s

Or you could simplify and just do this:

$html = <<<END_OF_HTML
<b>hello</b>
<b class="blah">hello</b>
<p>those</p>
<p class="blah">hello</p>
<a>hello</a>
<a href="url">this</a>
<a>rose</a>
<a href="regex yo">hello</a>
<a>nose</a>
<a id="2" href="regex yo">hello</a>
<p>that</p>
<p class="blah" title="whatever">hello</p>
END_OF_HTML;

// This will give you any tag
preg_match_all("/<[\s\S]*?>*?<\/[\s\S]*?>/", $html, $matches);
print_r($matches);

// This will give you any p tag
preg_match_all("/<p[\s\S]*?>*?<\/p[\s\S]*?>/", $html, $matches);
print_r($matches);
  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 04h52.


É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,13397 seconds with 14 queries