regex - PHP: preg_replace() "inch" doublequotes with double-singlequotes? -
regex - PHP: preg_replace() "inch" doublequotes with double-singlequotes? -
it's been couple years since used regex, , beingness novice then, i'm getting stumped on thought easy one.
i have 2 info sources need compare, , 1 has double-quotes " denoting inches while other uses doubled-single-quotes ''. i'm iterating through, need convert single-doubles " double-singles ''.
i started off playing preg_match(), , 1 time had pattern returning true, moved preg_replace(). looks feeble effort not holding up, though. let's take look:
$str = 'pre-301 1/8" id 48" bx'; preg_replace( '/(\d+)\"/', "\1''", $str ); echo '<pre>',var_dump($str),'</pre>'; my goal string(24) "pre-301 1/8'' id 48'' bx". getting, though, original string(22) "pre-301 1/8" id 48" bx" back.
you not assigning value back, , need alter \1 $1 in replacement.
$str = preg_replace('/(\d+)"/', "$1''", $str); live demo
note: can remove backslash before quotes, it's not neccessary here. , if strings this, can utilize replace quotes lone using str_replace():
$str = str_replace('"', "''", $str); php regex
Comments
Post a Comment