/    Sign up×
Community /Pin to ProfileBookmark

Need regex help

I am trying to write a regex which will recognize a block of text as being composed of plain text and HTML Comments. The regex I have so far is as follows:

[code=php]$pattern = “/(?P<text>.*?)(<!–(?P<comment>.*?)–>)?/sm”;[/code]

What I want is to get the first chunk of text into the back-reference <text> and get the text of the comment into the back-reference <comment>. I made <text> lazy, because it tries to take the comment also otherwise, and I made <comment> optional because it will not always be there. The problem, is that will this combination, it now captures nothing.

I cannot figure this out. Can anyone help?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bokehMay 04.2006 — Can you provide a block of text and highlight the parts you are trying to capture.
Copy linkTweet thisAlerts:
@aaronbdavisauthorMay 04.2006 — [CODE]this is some text <!-- and this is a comment-->
and this is some more text <!-- and this is another comment -->[/CODE]
I want to capture as follows
[CODE][text] => this is some text
[comment] => and this is a comment

// this would also be fine
[comment] => <!-- and this is a comment -->[/CODE]
I plan on using this in a while (preg_match(... loop, cutting away what I found previously in order to build an array of text and comment nodes.

i.e.[CODE]Array
{
[1_text] => this is some text
[2_comment] => and this is a comment
[3_text] => and this is some more text
[4_comment] => and this is another comment
}[/CODE]
Copy linkTweet thisAlerts:
@NogDogMay 04.2006 — Don't know if this would do the job for you:
[code=php]
<?php
header("Content-type: text/plain");
$text = <<<EOD
This is a test. <!-- This is a comment -->
This is only a test. <!-- This is another comment -->
<!-- This is a comment on its own line -->
This has been a test.
The end.
EOD;

preg_match_all('/<!--s*(.+)s*-->/U', $text, $matches);
$nonComment = preg_replace('/<!--s*(.+)s*-->/U', "t", $text);
$data['text'] = explode("t", $nonComment);
foreach($data['text'] as $key => $val)
{
$data['text'][$key] = trim($val);
}
$data['comment'] = $matches[1];
print_r($data);
?>
[/code]
Copy linkTweet thisAlerts:
@aaronbdavisauthorMay 04.2006 — I think I can make that work. Thanks NogDog.
×

Success!

Help @aaronbdavis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...