NYCPHP Meetup

NYPHP.org

[nycphp-talk] regexp: feeling stupid

David Sklar sklar at sklar.com
Wed Mar 17 14:23:29 EST 2004


The regular expression '|<sup 
class="gt_article_footnote">([^</sup>]+)</sup>|'

Says

"match the literal text '<sup class="gt_article_footnote">' then one or 
more characters that aren't <, /, s, u, p, or >, then the literal text 
'</sup>'"

You want

{<sup class="gt_article_footnote">(.+?)</sup>}

which says "match the literal text '<sup class="gt_article_footnote">' 
then as few characters as possible, then the literal text '</sup>'

(I also changed the delimiters to curly braces to avoid confusion with 
the | regex metacharacter.)

This regex still puts the stuff inside the <sup></sup> tags in $matches[1].

David


Marc Antony Vose wrote:

> $fn_text_to_match = '|<sup 
> class="gt_article_footnote">([^</sup>]+)</sup>|';
> $art_body_real = preg_replace_callback( 
> $fn_text_to_match,'handleFootnote',$art_body );
> 
> the function handleFootnote, does this:
> 
> $all_footnotes = array();
> function handleFootnote( $matches ){
>     global $fn_ctr, $all_footnotes, $art_id;
> 
>     //echo "we found a match: " . $fn_ctr . " | " . $matches[1] . "<br />";
>     $all_footnotes[$fn_ctr] = array(
>         "text" => $matches[1]
>     );
> 
>     $fn_ctr++;
> 
>     return '<sup class="gt_article_footnote"><a 
> href="javascript:showGTFootnotes(' . "'" . $fn_ctr . "'" . ')">' . 
> ($fn_ctr+1) . '</a></sup>';
> }
> ---
> 
> Basically, what's going on here, is the client has a tool where they can 
> enter text in superscript, then i need to parse that text out and 
> replace it with a number, and extract the text they had as a 'footnote' 
> which is exported to a text file.
> 
> But the problem with my original match is i need to match the <sup 
> class="gt_article_footnote">...</sup>
> 
> can anyone help me with that first line of code there?  i am not the 
> most experienced with regular expressions...





More information about the talk mailing list