I need to check if a string starts and ends with the "
. And if it is not the case, the "
should be added to it. e.g. text
convert into "text"
. I'm using the folling code for this:
if (strpos($owner_text, '"') !== 0)
{
$owner_text = '"' . $owner_text . '"';
}
then I realized, this will not work if I have something like text"
or "text
. In these cases, text"
will be converted into "text""
and "text
will stay untouched. So I change the code above:
// e.g. text. strpos returns false if nothing found
if (strpos($owner_text, '"') === false && strrpos($owner_text, '"') === false)
{
$owner_text = '"' . $owner_text . '"';
}
// "text
else if (strpos($owner_text, '"') === 0 && strrpos($owner_text, '"') === 0)
{
$owner_text = $owner_text . '"';
}
else if (strpos($owner_text, '"') !== 0 && strrpos($owner_text, '"') !== 0)
{
// text"
if (strpos($owner_text, '"') == strlen($owner_text) - 1)
{
$owner_text = '"' . $owner_text;
}
// te"xt
else
{
$owner_text = '"' . $owner_text . '"';
}
}
this works now with text
"text
or text"
, but it seems reaaaally complicated and it has problem with something like te"xt"
. How should I do this properly?