preg_match to pick words between single quotes and remove blanks from the end of match[1] with the same Pattern is possible?

Asked

Viewed 40 times

-1

According to the strings below, using preg_match, I need a Pattern that is able to separate what is inside and outside of simple quotes and remove $match[1] blank spaces from the end of the string. It is possible?

<?php

$string_1 = "Scorpions 'Scorpions Dynamite'";
$string_2 = "Scorpions 'Scorpions 'Dynamite''";

$pattern = "/(.*)'([^\']+)'/";

$s1 = preg_match ($patern, $string_1, $match);

$s2 = preg_match ($patern, $string_2, $match);

print_r ($s1);

print_r ($s2);
?>

Result of the above code:

s1 = array (
  0 => 'Scorpions \'Scorpions Dynamite\'',
  1 => 'Scorpions ',
  2 => 'Scorpions Dynamite',
)

s2 = array (
  0 => 'Scorpions \'Scorpions \'Dynamite\'',
  1 => 'Scorpions \'Scorpions ',
  2 => 'Dynamite',
)

With a lot of difficulty I arrived in this Pattern here, but it is not enough... I thank you for the help.

$pattern = "/(.*)'([^\']+)'/";

ADDENDUM :

What I need to do is modify the Pattern of such a foma that the $match[1] array will only show simple words, without blank spaces at the end [only alphanumeric characters], because as you can see, until a quote escaped to the match and reappeared in one of the indexes of the array and this gives error in a json application I have here.

So should be the exit on the two arrays:

array (
  0 => 'Scorpions \'Scorpions Dynamite\'',
  1 => 'Scorpions',
  2 => 'Scorpions Dynamite',
)

array (
  0 => 'Scorpions \'Scorpions \'Dynamite\'',
  1 => 'Scorpions Scorpions',
  2 => 'Dynamite',
)

I don’t know if I was clear, if you want to explain again

1 answer

1

Just for the record, your codes here don’t print arrays:

print_r ($s1);

print_r ($s2);

Although you state that the output is an array, and something else, in the variable name is $pattern, but you lost a t in:

preg_match ($patern, $string_1, $match);

About the question problem, really you don’t need make a monstrous, just treat extra spaces like:

1 => 'Scorpions ',

With the function trim iterating on a foreach, is simple:

<?php

$string_1 = "Scorpions 'Scorpions Dynamite'";
$string_2 = "Scorpions 'Scorpions 'Dynamite''";

$pattern = "/(.*)'([^\']+)'/";

$s1 = preg_match($pattern, $string_1, $match1);
$s2 = preg_match($pattern, $string_2, $match2);

foreach ($match1 as &$value) {
    $value = trim($value);
}

foreach ($match2 as &$value) {
    $value = trim($value);
}

var_dump($match1);

var_dump($match2);

I used var_dump to see the output in more detail, see an online example of the test: https://ideone.com/fTESzq

And note that the 0 which is the match all you can remove it with the function array_shift, thus:

array_shift($match1);
array_shift($match2);

That would result in:

array(2) {
  [0]=>
  string(9) "Scorpions"
  [1]=>
  string(18) "Scorpions Dynamite"
}
array(2) {
  [0]=>
  string(20) "Scorpions 'Scorpions"
  [1]=>
  &string(8) "Dynamite"
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.