Reputation: 2505
I have a web app which needs to parse string files from iOS apps. I am trying to find all instances of backslash escapes (\n, \t, \, etc) in a string, and add a span class to them, in PHP. For instance,
"Hello my name is Jim. \nI have \t12\tdogs"
Should become:
"Hello my name is Jim.<span class='b'>\n</span> and I have <span class='b'>\t</span>12<span class='b'>\t</span>dogs"
Unfortunately my regex skills are beyond rusty. I can't figure out how to not just replace the escapes, but add text on each side while retaining them. How can I do this correctly and efficiently? (will potentially be parsing 1000+ strings at a time)
EDIT: to clarify, I want to replace any possible escape characters, not just t and n. So any 2-character set that begins with a '\', including '\\'
Upvotes: 2
Views: 673
Reputation: 557
I think this might help you :
if you wanna match non-printable chars and backslash try this :
$newString = preg_replace('/[\s\a\e\\]/', '<span class="b">$1</span>', $oldString);
but if you wanna match all printable alpahnum chars that starts with backslash and the "\x**" format try this :
$newString = preg_replace('/\\[^[:punct:]\s]|\\\\|\\x[a-fA-F0-9]{1,2}/', '<span class="b">$1</span>', $oldString);
this match all chars after backslash expect punctuation chars and chars like \x23, \xAF ...
Upvotes: 0
Reputation: 4880
$newString = preg_replace('/(\\\\\S)/', '<span class="b">$1</span>', $oldString);
One thing to note, when you use \\
in a PHP string, the resulting string actually contains a single \
. \\\
and \\\\
will both result in \\
.
Upvotes: 0
Reputation: 7870
From the PHP documentation on Escape Characters:
Single and double quoted PHP strings have special meaning of backslash. Thus if \ has to be matched with a regular expression \\, then "\\\\" or '\\\\' must be used in PHP code.
This is an example with preg_replace
. Speed isn't an issue for this script.
$string = 'Hello my name is Jim. \nI have 1\\\2 of \t12\tdogs\r\n';
$pattern = '!(\\\\t|\\\\n|\\\\r|\\\\v|\\\\e|\\\\f|\\\\$|\\\\"|\\\\\\\\|\\\\\\\\\\\\\\\\|\\\\\\\\\\\\)!';
$replacement = "<span class='b'>$1</span>";
$string = preg_replace($pattern,$replacement,$string);
echo "<pre>$string</pre>";
Output
<pre>Hello my name is Jim. <span class='b'>\n</span>I have 1<span class='b'>\\</span>2 of <span class='b'>\t</span>12<span class='b'>\t</span>dogs<span class='b'>\r</span><span class='b'>\n</span></pre>
Upvotes: 1
Reputation: 101473
This is possible without regex, which is good for your speed requirement. A simple str_replace
will work:
$string = "Hello my name is Jim. \nI have \t12\tdogs";
$newString = str_replace(
array("\\t", "\\n"),
array("<span class=\"b\">\\t</span>", "<span class=\"b\">\\n</span>"),
$string
);
Upvotes: 4