1

I want to do some custom character escaping which means I want some characters to be preceded with some special character only if they are not already preceded by that special character. I assume I am going to need a regex replace. What is the easiest way to achieve that? I tried something with negative lookbehind assertion and ended up with nothing.

EDIT: Imagine that I want any of abc chars to be escaped with \, so giving debby I should get de\b\by while out of da\bby I want to make d\a\b\by. I guess this example is not that creative, nevertheless I believe it explains what I mean (escape chars that are not yet escaped)

EDIT2: Well, what I am actually trying to do is to turn a string of

([A-Za-z]+)/([A-Za-z]+)/([A-Za-z]+)

into

([A-Za-z]+)\/([A-Za-z]+)\/([A-Za-z]+)

In the string number one the forward slash is, say, some kind of a delimiter (and I expect to have more of them later, I would also like to escape ., , at the same time) which I need to escape with \ to use it as another (simpler) pattern. I tried (not sure about the second argument but that is not a problem I believe)

preg_replace("/(?<!\\)([\/])/", "\\$0", $string);

where $string is ([A-Za-z]+)/([A-Za-z]+)/([A-Za-z]+). No luck.

Hope I made that clear now

SOLUTION:

preg_replace("/(?<!\\\\)[\/]/", "\\\\$0", $string); in the square brackets I put the chars to be escaped.

patryk
  • 651
  • 12
  • 30
  • `preg_replace('/(yourchars)/', 'escapechars$1', $string)`? – Marc B Aug 08 '13 at 19:58
  • 2
    More details! Examples! What input? What output? – Sven Aug 08 '13 at 19:58
  • 2
    Can you show examples and what you tried? – ajp15243 Aug 08 '13 at 19:59
  • 1
    [What have you tried?](http://whathaveyoutried.com) The best way to get help is via a [Short, Self Contained, Compilable Example](http://sscce.org) – FrankieTheKneeMan Aug 08 '13 at 20:01
  • Edited. I tried something like `/(?<!\\)([abc])/` but that seems not to be proper regex – patryk Aug 08 '13 at 20:06
  • Your question is unclear and the examples are contradicting. Why is the `e` not escaped here `de\b\by` ? while the `a` is escaped in `d\a\b\by`. Also why is the first and last letter not escaped ? – HamZa Aug 08 '13 at 20:23
  • Because I want only the three `abc` to be escaped. More generally, I want only some characters to be escaped, in my low example those escaped were `abc` – patryk Aug 08 '13 at 20:26
  • @patryk, We need to see code. Your regex looks like it would work to me. Please post what you actually tried - a [Short, Self Contained, Compilable Example](http://sscce.org). – FrankieTheKneeMan Aug 08 '13 at 20:28
  • 1
    @patryk ok I see. Your expression is right, but it won't work in PHP. Use 4 backslashes: `/(?<!\\\\)([abc])/` , take a look at my early answer for some [explanation](http://stackoverflow.com/questions/18017742/php-preg-replace/18017821#18017821) – HamZa Aug 08 '13 at 20:30
  • I am confused. Considering my actual goal, what should the pattern look like? – patryk Aug 08 '13 at 20:40
  • I guess I made it with help of your explanation. Did not really expect backslashes needed to be escaped twice. Thank you for your help – patryk Aug 08 '13 at 20:48
  • @patryk If you found the solution then post it as an answer. Also next time if you're writing to someone, don't forget to ping him – HamZa Aug 08 '13 at 20:53
  • 1
    @HamZa Thank you for these tips, I not only seem to be a newbie here. – patryk Aug 08 '13 at 21:05

1 Answers1

6

I made a easy to use function to do this. It will escape any set of characters with any character or string. It'll also make it easy if you plan to change the characters to be escaped or the escape sequence in future.

function escapeChars($charsToEscape, $escapeSeq, $string)
{
    $charsToEscape = preg_quote($charsToEscape, '/');
    $regexSafeEscapeSeq = preg_quote($escapeSeq, '/');
    $escapeSeq = preg_replace('/([$\\\\])/', '\\\$1', $escapeSeq);
    return(preg_replace('/(?<!'.$regexSafeEscapeSeq.')(['.$charsToEscape.'])/', $escapeSeq.'$1', $string));
}

The parameters

$charsToEscape : The character(s) to be escaped

  • 'a' escapes just 'a'
  • 'abc' escapes 'a', 'b' and 'c'

$escapeSeq : The escape sequence character/string

  • Could be any character or string. Eg: '/', '#', '$', '$#/', '$$$', 'a', 'hello', etc.

  • To use '\' as escape sequence give '\\'

  • For '\\', give '\\\\' - because php considers a '\' preceding another "\" or a " ' " (if defining string with single quotes) or a ' " ' (if defining string with double quotes) as escape sequence

$string : The string from which you want to escape the characters

In your specific case, use

$string = '([A-Za-z]+)/([A-Za-z]+)/([A-Za-z]+)';    
echo escapeChars('/', '\\', $string);
Jithesh
  • 972
  • 4
  • 10
  • 1
    That is pretty much what I did, however you have generalized my case which is cool to me and I believe your answer explains my problem even better than my description itself. I am going to put your function in my little project. Thank you – patryk Aug 09 '13 at 08:07