Regex for removing spaces between start and end quote (PHP) -
i remove spaces if shown on start or end of name inside qotes. let me explain more on example:
this result have:
" hairdresser person 1" person 1 businesses
" hair studio " person 2 s.p.
" hairdresser person 3 " - person 3 s.p.
and i'm trying achieve:
"hairdresser person 1" person 1 businesses
"hair studio" person 2 s.p.
"hairdresser person 3" - person 3 s.p.
this code i'm working on:
$data[$num]['title'] = preg_replace(????????, "",$row->find('td',1)->plaintext);
if have better solution preg_replace i'm open it. thank guys in advance.
it isn't safe search quoted text need trimmed (for example: "abcd" efgh " ijkl "
can problematic approach , naive pattern).
a possible way consists search all quoted parts , trim them using preg_replace_callback
:
$data[$num]['title'] = preg_replace_callback('~"([^"]*)"~', function ($m) { return '"' . trim($m[1]) . '"'; }, $row->find('td', 1)->plaintext);
the advantage of way pattern used simple.
you can avoid preg_replace_callback
need more complicated pattern:
$data[$num]['title'] = preg_replace('~"\s*+([^\s"]*(?:\s+[^\s"]+)*+)\s*"~', '"$1"', $row->find('td', 1)->plaintext);
note these 2 patterns assume quotes balanced , quoted parts don't contain escaped quotes.
Comments
Post a Comment