preg_replace ... this time to complicate for me

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
th23
Forum Newbie
Posts: 1
Joined: Tue Mar 11, 2008 7:18 am

preg_replace ... this time to complicate for me

Post by th23 »

Hi, I might have a tricky (?) regex question here to be used in a php preg_replace function. I've done some easy ones thile coding, but this time I'm stuck...

Target:
  • Replace „test“ (always without quotation marks) by „<span class=”class1”>test</span>“, whereas “test” in the target should be a back reference to the found “test” and all this should be case in-sensitive
  • Test has to stand out as a sole word – at the beginning of the text, a line or after a whitespace / same goes for the end
  • If “test” is not between “[“ and “:0a1b2c3d]”, whereas “0a1b2c3d” can be any 8 digit/letter combination including capital characters
    AND
  • If “test” is not within “[url=http://www.test.com:0a1b2c3d]text[/url:0a1b2c3d]”, whereas for “0a1b2c3d” applies the same as above and “http://www.test.com” can be everything (combination of letter/numbers/special characters – the regex does not need to check for validity of the URL)
I looked at many examples and tutorials, yet I could not figure out the solution :-( I found parts of it, but I need your help there…

Thanks in advance!

The parts I figured out so far:
  • “/i” at the end of the search term will take care of the case in-sensivity
  • Back-reference is done by using “(test)” in the search expression and “\1” in the target
  • Word boundary is referenced to by “\b” in the search term
Hope to find the right expert(s) here ;-)

Cheers
th23
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: preg_replace ... this time to complicate for me

Post by GeertDD »

th23 wrote: Test has to stand out as a sole word – at the beginning of the text, a line or after a whitespace / same goes for the end
Here's another piece for your puzzle:

Code: Select all

 
(?<\s|^)test(?=\s|$)
 
Post Reply