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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -