views:

73

answers:

1

I'm trying to strip content titles out of the middle of text strings. Could I use regex to strip everything out of this string except for the title (in italics) in these strings? Or is there a better way?

Joe User wrote a blog post called The 10 Best Regex Expressions in the category Regex.

Jane User wrote a blog post called Regex is Hard! in the category TechProblems.

I've tried to come up with a regex expression to cover this, but I think it might need two. The trick is that the text in bold is always the same, so you could search for that, like this:

regex: delete everything before and including wrote a blog post called

regex: delete in the category and everything after it.

+1  A: 
^.+ wrote a blog post called (.+) in the category .+$

So:

preg_replace( '|^.+ wrote a blog post called (.+) in the category .+$|', '\1', $str );

^ is start of line
.+ is some characters (one to many)
(.+) same, but the content will be available in \1, for preg_replace()
$ is the end of line

Macmade
Perfect - thank you very much! This one was really vexing.
55skidoo