$string = "aaa, bbb, ccc, ddd, eee, fff";
I would like to cut string after third , so i would like to get output from string:
aaa, bbb, ccc
The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.
Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.
Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.
The str_split() function splits a string into an array.
$string = "aaa, bbb, ccc, ddd, eee, fff";
$arr = explode(", ", $string);
$arr = array_splice($arr, 0, 3);
$string = implode($arr, ", ");
echo $string; // = "aaa, bbb, ccc"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With