Im creating a function to check whether a string contains a specific character prefixed,I need to validate the text based on two conditions.
1.if the string contains character + prefixed ,i have to show the text in output without prefix +. eg: "we have dinner +today" output: "we have dinner today"
2.if the string contains character - prefixed ,i have to show the text in output removing the whole text prefixed with -. eg: "we have dinner -today" output: "we have dinner".
I will pass an extra parameter called length in this function ,If the string length is less than the given length then the string will be removed. eg: length=4; eg: "we have dinner -today" output: "have dinner". eg: "we have dinner +today" output: "have dinner today". The function i have created so far is
$fulltext="-through the +use of the +tab +key";
$length=4;
function checkstring($fulltext,$length)
{
$stringArray = explode(" ", $fulltext);
foreach ($stringArray as $value)
{
if(strlen($value) < $length)
$fulltext= str_replace(" ".$value." " ," ",$fulltext);
}
return $fulltext;
}
print_r(checkstring($fulltext,$length));
The output should be "use tab key"