0

Im having propbles doing this regex to get everything after a certain character (* in this case) and ends with a newline in PHP.

I have:

texttextext
*Sometext
*othertext

texttextex

i want to be able to get "*Sometext and "*othertext" and add <b></b> to them.

Already tried with

$a = array( 
      "/\*(.*?)\n/is"
   ); 
   $b = array( 
      "<b>$1</b>"
   );
   $texto = preg_replace($a, $b, $texto); 

but it does not work. What im doing wrong?

Homeroe
  • 1
  • 1

1 Answers1

1
"/^\*(.*?)\n/im"

You just have to add ^ to match the beginning of the line. This requires the /m multiline flag.

The /s flag however was wrong there, as that allowed .* to also match linebreaks - which you do not want.

mario
  • 144,265
  • 20
  • 237
  • 291
  • One last question. It seems to work with * character, but fails to if i use the ">" character – Homeroe Aug 29 '11 at 04:24
  • Not sure. Can you show the complete regex you've tried? (`>` is not a metacharacter and should work as-is) – mario Aug 29 '11 at 04:43
  • Also check out http://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world and http://stackoverflow.com/questions/2491930/is-there-an-online-regexbuddy-like-regular-expression-analyzer for some tools to verify. – mario Aug 29 '11 at 04:46