A string can come in single or double quotation marks. The goal is to get the chain without quotation marks. Delete only a couple of quotation marks, if more quotation marks appear, they will be part of the chain to be returned.
function removeQuotationMarks($string): string {
if( str_starts_with($string, '"') && str_ends_with($string, '"') ||
str_starts_with($string, "'") && str_ends_with($string, "'")
)
{
return substr($string,1,-1);
}
return $string;
}
<?php
use PHPUnit\Framework\TestCase;
// PHPUnit Test Examples:
// TODO: Replace examples and use TDD by writing your own tests
class ExampleTest extends TestCase
{
// test function names should start with "test"
public function testRemoveQuotationMarks() {
$this->assertEquals('a', removeQuotationMarks('"a"'));
$this->assertEquals('"a', removeQuotationMarks('"a'));
$this->assertEquals('a"', removeQuotationMarks('a"'));
$this->assertEquals('a', removeQuotationMarks("\"a\""));
$this->assertEquals('"a', removeQuotationMarks("\"a"));
$this->assertEquals('a"', removeQuotationMarks("a\""));
$this->assertEquals("a", removeQuotationMarks("'a'"));
$this->assertEquals("'a", removeQuotationMarks("'a"));
$this->assertEquals("a'", removeQuotationMarks("a'"));
$this->assertEquals("a", removeQuotationMarks('\'a\''));
$this->assertEquals("'a", removeQuotationMarks('\'a'));
$this->assertEquals("a'", removeQuotationMarks('a\''));
$this->assertEquals("'a''", removeQuotationMarks("''a'''"));
$this->assertEquals('""a"', removeQuotationMarks('"""a""'));
}
}