0

I'm trying to work with some regex in PHP but there is something i don't understand. Here is my text:

# fhzmvbzmvbzmb#@!
# blabla
# test
sbsfzzbg

And let's say i want to emphasise it as in markdown. Why does the following function apply to my second line only ? I would expect it to apply to the third line as well.

preg_replace("/\n(.*)\n/", "<h1>$1</h1>", $input_lines);

Also, i want to catch the first line. Is there a way to write the expression i am trying to catch could either be at the beginning of the string or not ? I've thought about the next function but it doesn't seem to work:

preg_replace("/(^|\n)(.*)\n/", "<h1>$1</h1>", $input_lines);

Thank you very much. Pierrick

arnaultp
  • 15
  • 5
  • This is not an answer - I will take a look and try to give you an answer - but one site you can take a look at is [regex101.com](http://www.regex101.com). It has a neat debugging feature that shows you where you code is matching and by inference where your code is not matching. – Quixrick Mar 16 '14 at 14:21
  • Thank you for your quick reply. Actually i've been using http://www.phpliveregex.com for my tests. My questions in here are purely curiosity oriented and to learn something that obviously i don't know. – arnaultp Mar 16 '14 at 14:25
  • what are you trying to match? – Amit Joki Mar 16 '14 at 14:31
  • This question has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Modifiers". – aliteralmind Apr 10 '14 at 00:55

2 Answers2

3

By using the m modifier, you can have ^ and $ apply to every line:

http://www.phpliveregex.com/p/4eb

From the documentation:

By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.

mrks
  • 6,996
  • 26
  • 48
  • Thanks a lot. Without knowing, that's what i was looking for. Where can i find (good ?) documentation about these parameters 'm', 'g' etc ? I've been following http://regexone.com/ to understand the basics of regex, but where find more ? – arnaultp Mar 16 '14 at 14:39
  • http://php.net/pcre, more specifically http://php.net/manual/en/reference.pcre.pattern.modifiers.php – mrks Mar 16 '14 at 14:40
0

To do the replacement, you can do something like this with lookaheads and lookbehinds to match the newlines. I'm not sure how you'd go about capturing the first line within the same expression you're using to replace. Here's what I came up with:

$input_lines = '# fhzmvbzmvbzmb#@!
# blabla
# test
sbsfzzbg';

// REPLACE
$data = preg_replace("/(?<=\n)(.*)(?=\n)/m", "<h1>$1</h1>", $input_lines);
print $data;

// GET THE FIRST LINE
preg_match('/^(.*)\n/', $input_lines, $first_line_matches);

print "\n\nFirst Line: ".$first_line_matches[1];

This outputs the following:

# fhzmvbzmvbzmb#@!
<h1># blabla
</h1>
<h1># test
</h1>
sbsfzzbg

First Line: # fhzmvbzmvbzmb#@!
Quixrick
  • 3,080
  • 1
  • 11
  • 15