tags:

views:

62

answers:

1

Hi, i need a regexp to match only single / in a string in PHP.

"bla bla bla/bla bla bla" //The regexp must match the / character
"bla bla // bla bla / bla bla" //The regexp must match only the last / not the first beacuse it is followed by another /

so i want only unescaped /.

+3  A: 

You can use zero-width assertions for this

{(?<!/)/(?!/)}

This matches a /, but only if not preceded by and not followed by another /

$escaped=preg_replace('{(?<!/)/(?!/)}', '//', $original);
Paul Dixon
I didn't know that...thanks
mck89