Valor_
Valor_

Reputation: 3591

Regex for removing spaces between start and end quote (PHP)

I would like to remove spaces if they are shown on start or end of a name inside qotes. Let me explain more on example:

This is the result i have:
" HAIRDRESSER PERSON 1" Person one businesses
" HAIR STUDIO " Person two S.P.
" HAIRDRESSER PERSON 3     " - PERSON 3 S.P.

And this is what i'm trying to achieve:
"HAIRDRESSER PERSON 1" Person one businesses
"HAIR STUDIO" Person two S.P.
"HAIRDRESSER PERSON 3" - PERSON 3 S.P.

This is the code i'm working on:

$data[$num]['title'] = preg_replace(????????, "",$row->find('td',1)->plaintext);

If you have any better solution then preg_replace i'm open for it. Thank you guys in advance.

Upvotes: 1

Views: 121

Answers (5)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

It isn't safe to only search quoted text that need to be trimmed (for example: "abcd" efgh " ijkl " can be problematic with this approach and a naive pattern).

A possible way consists to search all quoted parts and to 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 this way is that the pattern used is very simple.

You can also avoid preg_replace_callback but you need a more complicated pattern:

$data[$num]['title'] = preg_replace('~"\s*+([^\s"]*(?:\s+[^\s"]+)*+)\s*"~', '"$1"', $row->find('td', 1)->plaintext);

Note that these two patterns assume that quotes are balanced and that quoted parts don't contain escaped quotes.

Upvotes: 2

Sebri Zouhaier
Sebri Zouhaier

Reputation: 745

I used the following reg expression in javascript and it works

 replace(/(\"\s+)|(\s+\")/g,"\"")

Upvotes: 2

sandeepsure
sandeepsure

Reputation: 1115

One simple solution you are trick it.

  str_replace('" ','"',$string);

Upvotes: 1

chris85
chris85

Reputation: 23892

You could use preg_replace or preg_replace_callback with the trim function.

echo preg_replace_callback('~"(.*?)"~', function($matches) {
                                    return '"' . trim($matches[1]) . '"';
                            }, '"HAIRDRESSER PERSON 1" Person one businesses');

Output:

"HAIRDRESSER PERSON 1" Person one businesses

echo preg_replace('~"\h*(.*?)\h*"~', '"$1"', '"HAIRDRESSER PERSON 1" Person one businesses');

Output:

"HAIRDRESSER PERSON 1" Person one businesses

The \h* is any amount of horizontal whitespace between the " and the next ". Regex101 demo: https://regex101.com/r/mG2eG9/1

The preg_replace_callback probably isn't needed here unless you wanted to manipulate the quoted data further.

Upvotes: 2

user2762134
user2762134

Reputation:

$strings = array
(
    '" HAIRDRESSER PERSON 1" Person one businesses',
    '" HAIR STUDIO " Person two S.P.',
    '" HAIRDRESSER PERSON 3     " - PERSON 3 S.P'
);

foreach($strings as $string) {
    var_dump($string);
    $string = preg_replace_callback('/"(.*)"/i', function($match) { return sprintf('"%s"', trim($match[1])); }, $string);
    var_dump($string);
}

Returns

string '" HAIRDRESSER PERSON 1" Person one businesses' (length=45)
string '"HAIRDRESSER PERSON 1" Person one businesses' (length=44)
string '" HAIR STUDIO " Person two S.P.' (length=31)
string '"HAIR STUDIO" Person two S.P.' (length=29)
string '" HAIRDRESSER PERSON 3     " - PERSON 3 S.P' (length=43)
string '"HAIRDRESSER PERSON 3" - PERSON 3 S.P' (length=37)

Upvotes: 1

Related Questions